Add a form of addPreserved which takes a string argument, to allow passes
[oota-llvm.git] / lib / Transforms / Scalar / CodeGenLICM.cpp
1 //===- CodeGenLICM.cpp - LICM a function for code generation --------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This function performs late LICM, hoisting constants out of loops that
11 // are not valid immediates. It should not be followed by instcombine,
12 // because instcombine would quickly stuff the constants back into the loop.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #define DEBUG_TYPE "codegen-licm"
17 #include "llvm/Transforms/Scalar.h"
18 #include "llvm/Constants.h"
19 #include "llvm/DerivedTypes.h"
20 #include "llvm/Instructions.h"
21 #include "llvm/IntrinsicInst.h"
22 #include "llvm/LLVMContext.h"
23 #include "llvm/Analysis/LoopPass.h"
24 #include "llvm/Analysis/AliasAnalysis.h"
25 #include "llvm/ADT/DenseMap.h"
26 using namespace llvm;
27
28 namespace {
29   class CodeGenLICM : public LoopPass {
30     virtual bool runOnLoop(Loop *L, LPPassManager &LPM);
31     virtual void getAnalysisUsage(AnalysisUsage &AU) const;
32   public:
33     static char ID; // Pass identification, replacement for typeid
34     explicit CodeGenLICM() : LoopPass(&ID) {}
35   };
36 }
37
38 char CodeGenLICM::ID = 0;
39 static RegisterPass<CodeGenLICM> X("codegen-licm",
40                                    "hoist constants out of loops");
41
42 Pass *llvm::createCodeGenLICMPass() {
43   return new CodeGenLICM();
44 }
45
46 bool CodeGenLICM::runOnLoop(Loop *L, LPPassManager &) {
47   bool Changed = false;
48
49   // Only visit outermost loops.
50   if (L->getParentLoop()) return Changed;
51
52   Instruction *PreheaderTerm = L->getLoopPreheader()->getTerminator();
53   DenseMap<Constant *, BitCastInst *> HoistedConstants;
54
55   for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
56        I != E; ++I) {
57     BasicBlock *BB = *I;
58     for (BasicBlock::iterator BBI = BB->begin(), BBE = BB->end();
59          BBI != BBE; ++BBI) {
60       Instruction *I = BBI;
61       // TODO: For now, skip all intrinsic instructions, because some of them
62       // can require their operands to be constants, and we don't want to
63       // break that.
64       if (isa<IntrinsicInst>(I))
65         continue;
66       // LLVM represents fneg as -0.0-x; don't hoist the -0.0 out.
67       if (BinaryOperator::isFNeg(I) ||
68           BinaryOperator::isNeg(I) ||
69           BinaryOperator::isNot(I))
70         continue;
71       for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
72         // Don't hoist out switch case constants.
73         if (isa<SwitchInst>(I) && i == 1)
74           break;
75         // Don't hoist out shuffle masks.
76         if (isa<ShuffleVectorInst>(I) && i == 2)
77           break;
78         Value *Op = I->getOperand(i);
79         Constant *C = dyn_cast<Constant>(Op);
80         if (!C) continue;
81         // TODO: Ask the target which constants are legal. This would allow
82         // us to add support for hoisting ConstantInts and GlobalValues too.
83         if (isa<ConstantFP>(C) ||
84             isa<ConstantVector>(C) ||
85             isa<ConstantAggregateZero>(C)) {
86           BitCastInst *&BC = HoistedConstants[C];
87           if (!BC)
88             BC = new BitCastInst(C, C->getType(), "hoist", PreheaderTerm);
89           I->setOperand(i, BC);
90           Changed = true;
91         }
92       }
93     }
94   }
95
96   return Changed;
97 }
98
99 void CodeGenLICM::getAnalysisUsage(AnalysisUsage &AU) const {
100   // This pass preserves just about everything. List some popular things here.
101   AU.setPreservesCFG();
102   AU.addPreservedID(LoopSimplifyID);
103   AU.addPreserved<LoopInfo>();
104   AU.addPreserved<AliasAnalysis>();
105   AU.addPreserved("scalar-evolution");
106   AU.addPreserved("iv-users");
107   AU.addPreserved("lda");
108   AU.addPreserved("live-values");
109
110   // Hoisting requires a loop preheader.
111   AU.addRequiredID(LoopSimplifyID);
112 }