Local.cpp revision 051a950000e21935165db56695e35bade668193b
14d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner//===-- Local.cpp - Functions to perform local transformations ------------===//
2fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman//
3b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//                     The LLVM Compiler Infrastructure
4b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//
54ee451de366474b9c228b4e5fa573795a715216dChris Lattner// This file is distributed under the University of Illinois Open Source
64ee451de366474b9c228b4e5fa573795a715216dChris Lattner// License. See LICENSE.TXT for details.
7fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman//
8b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//===----------------------------------------------------------------------===//
94d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner//
104d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner// This family of functions perform various local transformations to the
114d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner// program.
124d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner//
134d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner//===----------------------------------------------------------------------===//
144d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner
154d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner#include "llvm/Transforms/Utils/Local.h"
1681ebc300891a81c305258aed980567514dff952dChris Lattner#include "llvm/Constants.h"
17c5f52e6da18e6e8ccb62aac2a4cb431df98e7d6dChris Lattner#include "llvm/DerivedTypes.h"
187822c2ae077429d7bf6eb3f6ebf99d61f359b601Chris Lattner#include "llvm/Instructions.h"
19cf11035a6f9973d68d8eaf837d71dcf272d36b79Chris Lattner#include "llvm/Intrinsics.h"
20741c0aea08feab0ebd1932aaa8dd38836b2073eaChris Lattner#include "llvm/IntrinsicInst.h"
21cbbc6b74e357afbf8fb37fdeb177ed78021092d3Chris Lattner#include "llvm/Analysis/ConstantFolding.h"
229fa038dc21e966dceb23f9410351e863e3ce1114Chris Lattner#include "llvm/Target/TargetData.h"
23c5f52e6da18e6e8ccb62aac2a4cb431df98e7d6dChris Lattner#include "llvm/Support/GetElementPtrTypeIterator.h"
24c5f52e6da18e6e8ccb62aac2a4cb431df98e7d6dChris Lattner#include "llvm/Support/MathExtras.h"
2509233fb86e237715d138db5dc5b72ada386089f2Alkis Evlogimenos#include <cerrno>
26abbc2dd77908f146f73f4cd1abfdfe47faacf43dChris Lattnerusing namespace llvm;
27d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
284d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner//===----------------------------------------------------------------------===//
2982c89b9f3a9b88bb63ce13b09b4f27fbb72f66fcMisha Brukman//  Local constant propagation...
304d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner//
314d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner
32abbc2dd77908f146f73f4cd1abfdfe47faacf43dChris Lattner/// doConstantPropagation - If an instruction references constants, try to fold
33abbc2dd77908f146f73f4cd1abfdfe47faacf43dChris Lattner/// them together...
34abbc2dd77908f146f73f4cd1abfdfe47faacf43dChris Lattner///
359fa038dc21e966dceb23f9410351e863e3ce1114Chris Lattnerbool llvm::doConstantPropagation(BasicBlock::iterator &II,
369fa038dc21e966dceb23f9410351e863e3ce1114Chris Lattner                                 const TargetData *TD) {
379fa038dc21e966dceb23f9410351e863e3ce1114Chris Lattner  if (Constant *C = ConstantFoldInstruction(II, TD)) {
384d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner    // Replaces all of the uses of a variable with uses of the constant.
3918961504fc2b299578dba817900a0696cf3ccc4dChris Lattner    II->replaceAllUsesWith(C);
40fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
414d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner    // Remove the instruction from the basic block...
4218961504fc2b299578dba817900a0696cf3ccc4dChris Lattner    II = II->getParent()->getInstList().erase(II);
434d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner    return true;
444d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner  }
454d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner
464d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner  return false;
474d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner}
484d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner
494d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner// ConstantFoldTerminator - If a terminator instruction is predicated on a
504d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner// constant value, convert it into an unconditional branch to the constant
514d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner// destination.
524d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner//
53abbc2dd77908f146f73f4cd1abfdfe47faacf43dChris Lattnerbool llvm::ConstantFoldTerminator(BasicBlock *BB) {
5476ae3445f81164aaff9f95123426109c119f27c0Chris Lattner  TerminatorInst *T = BB->getTerminator();
55fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
564d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner  // Branch - See if we are conditional jumping on constant
574d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner  if (BranchInst *BI = dyn_cast<BranchInst>(T)) {
584d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner    if (BI->isUnconditional()) return false;  // Can't optimize uncond branch
594d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner    BasicBlock *Dest1 = cast<BasicBlock>(BI->getOperand(0));
604d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner    BasicBlock *Dest2 = cast<BasicBlock>(BI->getOperand(1));
614d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner
626b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng    if (ConstantInt *Cond = dyn_cast<ConstantInt>(BI->getCondition())) {
634d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      // Are we branching on constant?
644d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      // YES.  Change to unconditional branch...
65579dca12c2cfd60bc18aaadbd5331897d48fec29Reid Spencer      BasicBlock *Destination = Cond->getZExtValue() ? Dest1 : Dest2;
66579dca12c2cfd60bc18aaadbd5331897d48fec29Reid Spencer      BasicBlock *OldDest     = Cond->getZExtValue() ? Dest2 : Dest1;
674d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner
68fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman      //cerr << "Function: " << T->getParent()->getParent()
69fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman      //     << "\nRemoving branch from " << T->getParent()
704d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      //     << "\n\nTo: " << OldDest << endl;
714d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner
724d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      // Let the basic block know that we are letting go of it.  Based on this,
734d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      // it will adjust it's PHI nodes.
744d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      assert(BI->getParent() && "Terminator not inserted in block!");
754d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      OldDest->removePredecessor(BI->getParent());
764d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner
774d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      // Set the unconditional destination, and change the insn to be an
784d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      // unconditional branch.
794d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      BI->setUnconditionalDest(Destination);
804d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      return true;
81342a9d1464ed3483b45b0ca47c0b130ba080c938Chris Lattner    } else if (Dest2 == Dest1) {       // Conditional branch to same location?
82fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman      // This branch matches something like this:
834d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      //     br bool %cond, label %Dest, label %Dest
844d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      // and changes it into:  br label %Dest
854d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner
864d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      // Let the basic block know that we are letting go of one copy of it.
874d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      assert(BI->getParent() && "Terminator not inserted in block!");
884d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      Dest1->removePredecessor(BI->getParent());
894d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner
904d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      // Change a conditional branch to unconditional.
914d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      BI->setUnconditionalDest(Dest1);
924d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner      return true;
934d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner    }
9410b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner  } else if (SwitchInst *SI = dyn_cast<SwitchInst>(T)) {
9510b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner    // If we are switching on a constant, we can convert the switch into a
9610b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner    // single branch instruction!
9710b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner    ConstantInt *CI = dyn_cast<ConstantInt>(SI->getCondition());
9810b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner    BasicBlock *TheOnlyDest = SI->getSuccessor(0);  // The default dest
997d6c24cdbf41522818ec9ae7b8d3b624660853c1Chris Lattner    BasicBlock *DefaultDest = TheOnlyDest;
1007d6c24cdbf41522818ec9ae7b8d3b624660853c1Chris Lattner    assert(TheOnlyDest == SI->getDefaultDest() &&
1017d6c24cdbf41522818ec9ae7b8d3b624660853c1Chris Lattner           "Default destination is not successor #0?");
10210b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner
10310b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner    // Figure out which case it goes to...
10410b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner    for (unsigned i = 1, e = SI->getNumSuccessors(); i != e; ++i) {
10510b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner      // Found case matching a constant operand?
10610b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner      if (SI->getSuccessorValue(i) == CI) {
10710b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner        TheOnlyDest = SI->getSuccessor(i);
10810b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner        break;
10910b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner      }
11010b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner
1117d6c24cdbf41522818ec9ae7b8d3b624660853c1Chris Lattner      // Check to see if this branch is going to the same place as the default
1127d6c24cdbf41522818ec9ae7b8d3b624660853c1Chris Lattner      // dest.  If so, eliminate it as an explicit compare.
1137d6c24cdbf41522818ec9ae7b8d3b624660853c1Chris Lattner      if (SI->getSuccessor(i) == DefaultDest) {
1147d6c24cdbf41522818ec9ae7b8d3b624660853c1Chris Lattner        // Remove this entry...
1157d6c24cdbf41522818ec9ae7b8d3b624660853c1Chris Lattner        DefaultDest->removePredecessor(SI->getParent());
1167d6c24cdbf41522818ec9ae7b8d3b624660853c1Chris Lattner        SI->removeCase(i);
1177d6c24cdbf41522818ec9ae7b8d3b624660853c1Chris Lattner        --i; --e;  // Don't skip an entry...
1187d6c24cdbf41522818ec9ae7b8d3b624660853c1Chris Lattner        continue;
1197d6c24cdbf41522818ec9ae7b8d3b624660853c1Chris Lattner      }
1207d6c24cdbf41522818ec9ae7b8d3b624660853c1Chris Lattner
12110b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner      // Otherwise, check to see if the switch only branches to one destination.
12210b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner      // We do this by reseting "TheOnlyDest" to null when we find two non-equal
12310b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner      // destinations.
12410b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner      if (SI->getSuccessor(i) != TheOnlyDest) TheOnlyDest = 0;
12510b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner    }
126694e37f08a7c09ccc24642532106295cf7b3a1e3Chris Lattner
12710b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner    if (CI && !TheOnlyDest) {
12810b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner      // Branching on a constant, but not any of the cases, go to the default
12910b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner      // successor.
13010b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner      TheOnlyDest = SI->getDefaultDest();
131694e37f08a7c09ccc24642532106295cf7b3a1e3Chris Lattner    }
132694e37f08a7c09ccc24642532106295cf7b3a1e3Chris Lattner
13310b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner    // If we found a single destination that we can fold the switch into, do so
13410b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner    // now.
13510b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner    if (TheOnlyDest) {
13610b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner      // Insert the new branch..
137051a950000e21935165db56695e35bade668193bGabor Greif      BranchInst::Create(TheOnlyDest, SI);
13810b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner      BasicBlock *BB = SI->getParent();
13910b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner
14010b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner      // Remove entries from PHI nodes which we no longer branch to...
14110b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner      for (unsigned i = 0, e = SI->getNumSuccessors(); i != e; ++i) {
14210b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner        // Found case matching a constant operand?
14310b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner        BasicBlock *Succ = SI->getSuccessor(i);
14410b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner        if (Succ == TheOnlyDest)
14510b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner          TheOnlyDest = 0;  // Don't modify the first branch to TheOnlyDest
14610b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner        else
14710b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner          Succ->removePredecessor(BB);
14810b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner      }
14910b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner
15010b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner      // Delete the old switch...
15110b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner      BB->getInstList().erase(SI);
15210b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner      return true;
15310b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner    } else if (SI->getNumSuccessors() == 2) {
15410b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner      // Otherwise, we can fold this switch into a conditional branch
15510b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner      // instruction if it has only one non-default destination.
156e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer      Value *Cond = new ICmpInst(ICmpInst::ICMP_EQ, SI->getCondition(),
157e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer                                 SI->getSuccessorValue(1), "cond", SI);
15810b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner      // Insert the new branch...
159051a950000e21935165db56695e35bade668193bGabor Greif      BranchInst::Create(SI->getSuccessor(1), SI->getSuccessor(0), Cond, SI);
16010b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner
16110b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner      // Delete the old switch...
16210b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner      SI->getParent()->getInstList().erase(SI);
16310b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner      return true;
16410b1f5a94196f27c75c950ba7ed26bd0a62c91e9Chris Lattner    }
1654d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner  }
1664d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner  return false;
1674d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner}
1684d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner
1694d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner
1704d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner//===----------------------------------------------------------------------===//
1714d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner//  Local dead code elimination...
1724d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner//
1734d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner
174abbc2dd77908f146f73f4cd1abfdfe47faacf43dChris Lattnerbool llvm::isInstructionTriviallyDead(Instruction *I) {
175ec710c5b12af647ae90f53917122726269c18738Chris Lattner  if (!I->use_empty() || isa<TerminatorInst>(I)) return false;
17600b16889ab461b7ecef1c91ade101186b7f1fce2Jeff Cohen
177741c0aea08feab0ebd1932aaa8dd38836b2073eaChris Lattner  if (!I->mayWriteToMemory())
178741c0aea08feab0ebd1932aaa8dd38836b2073eaChris Lattner    return true;
179ec710c5b12af647ae90f53917122726269c18738Chris Lattner
180741c0aea08feab0ebd1932aaa8dd38836b2073eaChris Lattner  // Special case intrinsics that "may write to memory" but can be deleted when
181741c0aea08feab0ebd1932aaa8dd38836b2073eaChris Lattner  // dead.
182741c0aea08feab0ebd1932aaa8dd38836b2073eaChris Lattner  if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
183741c0aea08feab0ebd1932aaa8dd38836b2073eaChris Lattner    // Safe to delete llvm.stacksave if dead.
184741c0aea08feab0ebd1932aaa8dd38836b2073eaChris Lattner    if (II->getIntrinsicID() == Intrinsic::stacksave)
185741c0aea08feab0ebd1932aaa8dd38836b2073eaChris Lattner      return true;
186741c0aea08feab0ebd1932aaa8dd38836b2073eaChris Lattner
187ec710c5b12af647ae90f53917122726269c18738Chris Lattner  return false;
1884d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner}
1894d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner
1904d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner// dceInstruction - Inspect the instruction at *BBI and figure out if it's
1914d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner// [trivially] dead.  If so, remove the instruction and update the iterator
1924d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner// to point to the instruction that immediately succeeded the original
1934d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner// instruction.
1944d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner//
195abbc2dd77908f146f73f4cd1abfdfe47faacf43dChris Lattnerbool llvm::dceInstruction(BasicBlock::iterator &BBI) {
1964d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner  // Look for un"used" definitions...
19718961504fc2b299578dba817900a0696cf3ccc4dChris Lattner  if (isInstructionTriviallyDead(BBI)) {
19818961504fc2b299578dba817900a0696cf3ccc4dChris Lattner    BBI = BBI->getParent()->getInstList().erase(BBI);   // Bye bye
1994d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner    return true;
2004d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner  }
2014d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner  return false;
2024d1e46e7b06534cde262d32fad038135f406b6b7Chris Lattner}
203