blob: 7f8df58db52edc401a35710634ee3dd4611b632b [file] [log] [blame]
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001//===-- Instructions.cpp - Implement the LLVM instructions ----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00008//===----------------------------------------------------------------------===//
9//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000010// This file implements all of the non-inline methods for the LLVM instruction
11// classes.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000012//
13//===----------------------------------------------------------------------===//
14
Devang Pateladd58652009-09-23 18:32:25 +000015#include "LLVMContextImpl.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000016#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/Function.h"
19#include "llvm/Instructions.h"
Evan Cheng1d9d4bd2009-09-10 04:36:43 +000020#include "llvm/Module.h"
Dan Gohmand2a251f2009-07-17 21:33:58 +000021#include "llvm/Operator.h"
Dan Gohman22571482009-09-03 15:34:35 +000022#include "llvm/Analysis/Dominators.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000023#include "llvm/Support/ErrorHandling.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000024#include "llvm/Support/CallSite.h"
Reid Spencer0286bc12007-02-28 22:00:54 +000025#include "llvm/Support/ConstantRange.h"
Christopher Lamb84485702007-04-22 19:24:39 +000026#include "llvm/Support/MathExtras.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000027using namespace llvm;
28
Chris Lattner3e13b8c2008-01-02 23:42:30 +000029//===----------------------------------------------------------------------===//
30// CallSite Class
31//===----------------------------------------------------------------------===//
32
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000033User::op_iterator CallSite::getCallee() const {
34 Instruction *II(getInstruction());
35 return isCall()
Gabor Greif638c8232010-08-05 21:25:49 +000036 ? cast<CallInst>(II)->op_end() - 1 // Skip Callee
Gabor Greif6d673952010-07-16 09:38:02 +000037 : cast<InvokeInst>(II)->op_end() - 3; // Skip BB, BB, Callee
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000038}
39
Gordon Henriksen14a55692007-12-10 02:14:30 +000040//===----------------------------------------------------------------------===//
41// TerminatorInst Class
42//===----------------------------------------------------------------------===//
43
44// Out of line virtual method, so the vtable, etc has a home.
45TerminatorInst::~TerminatorInst() {
46}
47
Gabor Greiff6caff662008-05-10 08:32:32 +000048//===----------------------------------------------------------------------===//
49// UnaryInstruction Class
50//===----------------------------------------------------------------------===//
51
Gordon Henriksen14a55692007-12-10 02:14:30 +000052// Out of line virtual method, so the vtable, etc has a home.
53UnaryInstruction::~UnaryInstruction() {
54}
55
Chris Lattnerafdb3de2005-01-29 00:35:16 +000056//===----------------------------------------------------------------------===//
Chris Lattner88107952008-12-29 00:12:50 +000057// SelectInst Class
58//===----------------------------------------------------------------------===//
59
60/// areInvalidOperands - Return a string if the specified operands are invalid
61/// for a select operation, otherwise return null.
62const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) {
63 if (Op1->getType() != Op2->getType())
64 return "both values to select must have same type";
65
66 if (const VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
67 // Vector select.
Owen Anderson55f1c092009-08-13 21:58:54 +000068 if (VT->getElementType() != Type::getInt1Ty(Op0->getContext()))
Chris Lattner88107952008-12-29 00:12:50 +000069 return "vector select condition element type must be i1";
70 const VectorType *ET = dyn_cast<VectorType>(Op1->getType());
71 if (ET == 0)
72 return "selected values for vector select must be vectors";
73 if (ET->getNumElements() != VT->getNumElements())
74 return "vector select requires selected vectors to have "
75 "the same vector length as select condition";
Owen Anderson55f1c092009-08-13 21:58:54 +000076 } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) {
Chris Lattner88107952008-12-29 00:12:50 +000077 return "select condition must be i1 or <n x i1>";
78 }
79 return 0;
80}
81
82
83//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000084// PHINode Class
85//===----------------------------------------------------------------------===//
86
87PHINode::PHINode(const PHINode &PN)
88 : Instruction(PN.getType(), Instruction::PHI,
Gabor Greiff6caff662008-05-10 08:32:32 +000089 allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()),
Chris Lattnerafdb3de2005-01-29 00:35:16 +000090 ReservedSpace(PN.getNumOperands()) {
91 Use *OL = OperandList;
92 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +000093 OL[i] = PN.getOperand(i);
94 OL[i+1] = PN.getOperand(i+1);
Chris Lattnerafdb3de2005-01-29 00:35:16 +000095 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +000096 SubclassOptionalData = PN.SubclassOptionalData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +000097}
98
Gordon Henriksen14a55692007-12-10 02:14:30 +000099PHINode::~PHINode() {
Chris Lattner7d6aac82008-06-16 04:02:40 +0000100 if (OperandList)
101 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000102}
103
104// removeIncomingValue - Remove an incoming value. This is useful if a
105// predecessor basic block is deleted.
106Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
107 unsigned NumOps = getNumOperands();
108 Use *OL = OperandList;
109 assert(Idx*2 < NumOps && "BB not in PHI node!");
110 Value *Removed = OL[Idx*2];
111
112 // Move everything after this operand down.
113 //
114 // FIXME: we could just swap with the end of the list, then erase. However,
115 // client might not expect this to happen. The code as it is thrashes the
116 // use/def lists, which is kinda lame.
117 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
118 OL[i-2] = OL[i];
119 OL[i-2+1] = OL[i+1];
120 }
121
122 // Nuke the last value.
123 OL[NumOps-2].set(0);
124 OL[NumOps-2+1].set(0);
125 NumOperands = NumOps-2;
126
127 // If the PHI node is dead, because it has zero entries, nuke it now.
128 if (NumOps == 2 && DeletePHIIfEmpty) {
129 // If anyone is using this PHI, make them use a dummy value instead...
Owen Andersonb292b8c2009-07-30 23:03:37 +0000130 replaceAllUsesWith(UndefValue::get(getType()));
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000131 eraseFromParent();
132 }
133 return Removed;
134}
135
136/// resizeOperands - resize operands - This adjusts the length of the operands
137/// list according to the following behavior:
138/// 1. If NumOps == 0, grow the operand list in response to a push_back style
139/// of operation. This grows the number of ops by 1.5 times.
140/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
141/// 3. If NumOps == NumOperands, trim the reserved space.
142///
143void PHINode::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000144 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000145 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000146 NumOps = e*3/2;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000147 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
148 } else if (NumOps*2 > NumOperands) {
149 // No resize needed.
150 if (ReservedSpace >= NumOps) return;
151 } else if (NumOps == NumOperands) {
152 if (ReservedSpace == NumOps) return;
153 } else {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000154 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000155 }
156
157 ReservedSpace = NumOps;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000158 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +0000159 Use *NewOps = allocHungoffUses(NumOps);
Dan Gohman031f0bb2008-06-23 16:45:24 +0000160 std::copy(OldOps, OldOps + e, NewOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000161 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +0000162 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000163}
164
Nate Begemanb3923212005-08-04 23:24:19 +0000165/// hasConstantValue - If the specified PHI node always merges together the same
166/// value, return the value, otherwise return null.
167///
Dan Gohman22571482009-09-03 15:34:35 +0000168/// If the PHI has undef operands, but all the rest of the operands are
169/// some unique value, return that value if it can be proved that the
170/// value dominates the PHI. If DT is null, use a conservative check,
171/// otherwise use DT to test for dominance.
172///
173Value *PHINode::hasConstantValue(DominatorTree *DT) const {
Chris Lattner7d08da62009-09-21 22:27:34 +0000174 // If the PHI node only has one incoming value, eliminate the PHI node.
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000175 if (getNumIncomingValues() == 1) {
Chris Lattner6e709c12005-08-05 15:37:31 +0000176 if (getIncomingValue(0) != this) // not X = phi X
177 return getIncomingValue(0);
Chris Lattner7d08da62009-09-21 22:27:34 +0000178 return UndefValue::get(getType()); // Self cycle is dead.
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000179 }
Chris Lattner6e709c12005-08-05 15:37:31 +0000180
Nate Begemanb3923212005-08-04 23:24:19 +0000181 // Otherwise if all of the incoming values are the same for the PHI, replace
182 // the PHI node with the incoming value.
183 //
184 Value *InVal = 0;
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000185 bool HasUndefInput = false;
Nate Begemanb3923212005-08-04 23:24:19 +0000186 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000187 if (isa<UndefValue>(getIncomingValue(i))) {
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000188 HasUndefInput = true;
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000189 } else if (getIncomingValue(i) != this) { // Not the PHI node itself...
Nate Begemanb3923212005-08-04 23:24:19 +0000190 if (InVal && getIncomingValue(i) != InVal)
191 return 0; // Not the same, bail out.
Chris Lattner7d08da62009-09-21 22:27:34 +0000192 InVal = getIncomingValue(i);
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000193 }
Nate Begemanb3923212005-08-04 23:24:19 +0000194
195 // The only case that could cause InVal to be null is if we have a PHI node
196 // that only has entries for itself. In this case, there is no entry into the
197 // loop, so kill the PHI.
198 //
Owen Andersonb292b8c2009-07-30 23:03:37 +0000199 if (InVal == 0) InVal = UndefValue::get(getType());
Nate Begemanb3923212005-08-04 23:24:19 +0000200
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000201 // If we have a PHI node like phi(X, undef, X), where X is defined by some
202 // instruction, we cannot always return X as the result of the PHI node. Only
203 // do this if X is not an instruction (thus it must dominate the PHI block),
204 // or if the client is prepared to deal with this possibility.
Chris Lattner7d08da62009-09-21 22:27:34 +0000205 if (!HasUndefInput || !isa<Instruction>(InVal))
206 return InVal;
207
208 Instruction *IV = cast<Instruction>(InVal);
209 if (DT) {
210 // We have a DominatorTree. Do a precise test.
211 if (!DT->dominates(IV, this))
212 return 0;
213 } else {
214 // If it is in the entry block, it obviously dominates everything.
215 if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
216 isa<InvokeInst>(IV))
217 return 0; // Cannot guarantee that InVal dominates this PHINode.
218 }
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000219
Nate Begemanb3923212005-08-04 23:24:19 +0000220 // All of the incoming values are the same, return the value now.
221 return InVal;
222}
223
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000224
225//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000226// CallInst Implementation
227//===----------------------------------------------------------------------===//
228
Gordon Henriksen14a55692007-12-10 02:14:30 +0000229CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000230}
231
Chris Lattner054ba2c2007-02-13 00:58:44 +0000232void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000233 assert(NumOperands == NumParams+1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000234 Op<-1>() = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000235
Misha Brukmanb1c93172005-04-21 23:48:37 +0000236 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000237 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000238 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000239
Chris Lattner054ba2c2007-02-13 00:58:44 +0000240 assert((NumParams == FTy->getNumParams() ||
241 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000242 "Calling a function with bad signature!");
Chris Lattner054ba2c2007-02-13 00:58:44 +0000243 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000244 assert((i >= FTy->getNumParams() ||
245 FTy->getParamType(i) == Params[i]->getType()) &&
246 "Calling a function with a bad signature!");
Gabor Greif6d673952010-07-16 09:38:02 +0000247 OperandList[i] = Params[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000248 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000249}
250
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000251void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000252 assert(NumOperands == 3 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000253 Op<-1>() = Func;
254 Op<0>() = Actual1;
255 Op<1>() = Actual2;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000256
257 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000258 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000259 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000260
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000261 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000262 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000263 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000264 assert((0 >= FTy->getNumParams() ||
265 FTy->getParamType(0) == Actual1->getType()) &&
266 "Calling a function with a bad signature!");
267 assert((1 >= FTy->getNumParams() ||
268 FTy->getParamType(1) == Actual2->getType()) &&
269 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000270}
271
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000272void CallInst::init(Value *Func, Value *Actual) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000273 assert(NumOperands == 2 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000274 Op<-1>() = Func;
275 Op<0>() = Actual;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000276
277 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000278 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000279 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000280
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000281 assert((FTy->getNumParams() == 1 ||
282 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000283 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000284 assert((0 == FTy->getNumParams() ||
285 FTy->getParamType(0) == Actual->getType()) &&
286 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000287}
288
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000289void CallInst::init(Value *Func) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000290 assert(NumOperands == 1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000291 Op<-1>() = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000292
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000293 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000294 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000295 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000296
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000297 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000298}
299
Daniel Dunbar4975db62009-07-25 04:41:11 +0000300CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +0000301 Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000302 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
303 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000304 Instruction::Call,
305 OperandTraits<CallInst>::op_end(this) - 2,
306 2, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000307 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000308 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000309}
310
Daniel Dunbar4975db62009-07-25 04:41:11 +0000311CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000312 BasicBlock *InsertAtEnd)
313 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
314 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000315 Instruction::Call,
316 OperandTraits<CallInst>::op_end(this) - 2,
317 2, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000318 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000319 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000320}
Daniel Dunbar4975db62009-07-25 04:41:11 +0000321CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000322 Instruction *InsertBefore)
323 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
324 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000325 Instruction::Call,
326 OperandTraits<CallInst>::op_end(this) - 1,
327 1, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000328 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000329 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000330}
331
Daniel Dunbar4975db62009-07-25 04:41:11 +0000332CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000333 BasicBlock *InsertAtEnd)
334 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
335 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000336 Instruction::Call,
337 OperandTraits<CallInst>::op_end(this) - 1,
338 1, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000339 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000340 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000341}
342
Misha Brukmanb1c93172005-04-21 23:48:37 +0000343CallInst::CallInst(const CallInst &CI)
Gabor Greiff6caff662008-05-10 08:32:32 +0000344 : Instruction(CI.getType(), Instruction::Call,
345 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000346 CI.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000347 setAttributes(CI.getAttributes());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000348 setTailCall(CI.isTailCall());
349 setCallingConv(CI.getCallingConv());
350
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000351 Use *OL = OperandList;
352 Use *InOL = CI.OperandList;
353 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000354 OL[i] = InOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000355 SubclassOptionalData = CI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000356}
357
Devang Patel4c758ea2008-09-25 21:00:45 +0000358void CallInst::addAttribute(unsigned i, Attributes attr) {
359 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000360 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000361 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000362}
363
Devang Patel4c758ea2008-09-25 21:00:45 +0000364void CallInst::removeAttribute(unsigned i, Attributes attr) {
365 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000366 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000367 setAttributes(PAL);
Duncan Sands78c88722008-07-08 08:38:44 +0000368}
369
Devang Patelba3fa6c2008-09-23 23:03:40 +0000370bool CallInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000371 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000372 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000373 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000374 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000375 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000376}
377
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000378/// IsConstantOne - Return true only if val is constant int 1
379static bool IsConstantOne(Value *val) {
380 assert(val && "IsConstantOne does not work with NULL val");
381 return isa<ConstantInt>(val) && cast<ConstantInt>(val)->isOne();
382}
383
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000384static Instruction *createMalloc(Instruction *InsertBefore,
385 BasicBlock *InsertAtEnd, const Type *IntPtrTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000386 const Type *AllocTy, Value *AllocSize,
387 Value *ArraySize, Function *MallocF,
388 const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000389 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000390 "createMalloc needs either InsertBefore or InsertAtEnd");
391
392 // malloc(type) becomes:
393 // bitcast (i8* malloc(typeSize)) to type*
394 // malloc(type, arraySize) becomes:
395 // bitcast (i8 *malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000396 if (!ArraySize)
397 ArraySize = ConstantInt::get(IntPtrTy, 1);
398 else if (ArraySize->getType() != IntPtrTy) {
399 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000400 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
401 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000402 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000403 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
404 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000405 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000406
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000407 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000408 if (IsConstantOne(AllocSize)) {
409 AllocSize = ArraySize; // Operand * 1 = Operand
410 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
411 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
412 false /*ZExt*/);
413 // Malloc arg is constant product of type size and array size
414 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
415 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000416 // Multiply type size by the array size...
417 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000418 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
419 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000420 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000421 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
422 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000423 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000424 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000425
Victor Hernandez788eaab2009-09-18 19:20:02 +0000426 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000427 // Create the call to Malloc.
428 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
429 Module* M = BB->getParent()->getParent();
Duncan Sands9ed7b162009-10-06 15:40:36 +0000430 const Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezbb336a12009-11-10 19:53:28 +0000431 Value *MallocFunc = MallocF;
432 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000433 // prototype malloc as "void *malloc(size_t)"
Victor Hernandezbb336a12009-11-10 19:53:28 +0000434 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, NULL);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000435 const PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000436 CallInst *MCall = NULL;
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000437 Instruction *Result = NULL;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000438 if (InsertBefore) {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000439 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall", InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000440 Result = MCall;
441 if (Result->getType() != AllocPtrType)
442 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000443 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000444 } else {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000445 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000446 Result = MCall;
447 if (Result->getType() != AllocPtrType) {
448 InsertAtEnd->getInstList().push_back(MCall);
449 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000450 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000451 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000452 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000453 MCall->setTailCall();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000454 if (Function *F = dyn_cast<Function>(MallocFunc)) {
455 MCall->setCallingConv(F->getCallingConv());
456 if (!F->doesNotAlias(0)) F->setDoesNotAlias(0);
457 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000458 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000459
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000460 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000461}
462
463/// CreateMalloc - Generate the IR for a call to malloc:
464/// 1. Compute the malloc call's argument as the specified type's size,
465/// possibly multiplied by the array size if the array size is not
466/// constant 1.
467/// 2. Call malloc with that argument.
468/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000469Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
470 const Type *IntPtrTy, const Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000471 Value *AllocSize, Value *ArraySize,
Duncan Sands41b4a6b2010-07-12 08:16:59 +0000472 Function * MallocF,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000473 const Twine &Name) {
474 return createMalloc(InsertBefore, NULL, IntPtrTy, AllocTy, AllocSize,
Chris Lattner601e390a2010-07-12 00:57:28 +0000475 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000476}
477
478/// CreateMalloc - Generate the IR for a call to malloc:
479/// 1. Compute the malloc call's argument as the specified type's size,
480/// possibly multiplied by the array size if the array size is not
481/// constant 1.
482/// 2. Call malloc with that argument.
483/// 3. Bitcast the result of the malloc call to the specified type.
484/// Note: This function does not add the bitcast to the basic block, that is the
485/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000486Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
487 const Type *IntPtrTy, const Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000488 Value *AllocSize, Value *ArraySize,
489 Function *MallocF, const Twine &Name) {
490 return createMalloc(NULL, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000491 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000492}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000493
Victor Hernandeze2971492009-10-24 04:23:03 +0000494static Instruction* createFree(Value* Source, Instruction *InsertBefore,
495 BasicBlock *InsertAtEnd) {
496 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
497 "createFree needs either InsertBefore or InsertAtEnd");
Duncan Sands19d0b472010-02-16 11:11:14 +0000498 assert(Source->getType()->isPointerTy() &&
Victor Hernandeze2971492009-10-24 04:23:03 +0000499 "Can not free something of nonpointer type!");
500
501 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
502 Module* M = BB->getParent()->getParent();
503
504 const Type *VoidTy = Type::getVoidTy(M->getContext());
505 const Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
506 // prototype free as "void free(void*)"
Chris Lattner2156c222009-11-09 07:12:01 +0000507 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, NULL);
Victor Hernandeze2971492009-10-24 04:23:03 +0000508 CallInst* Result = NULL;
509 Value *PtrCast = Source;
510 if (InsertBefore) {
511 if (Source->getType() != IntPtrTy)
512 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
513 Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore);
514 } else {
515 if (Source->getType() != IntPtrTy)
516 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
517 Result = CallInst::Create(FreeFunc, PtrCast, "");
518 }
519 Result->setTailCall();
Chris Lattner2156c222009-11-09 07:12:01 +0000520 if (Function *F = dyn_cast<Function>(FreeFunc))
521 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000522
523 return Result;
524}
525
526/// CreateFree - Generate the IR for a call to the builtin free function.
Chris Lattner601e390a2010-07-12 00:57:28 +0000527Instruction * CallInst::CreateFree(Value* Source, Instruction *InsertBefore) {
528 return createFree(Source, InsertBefore, NULL);
Victor Hernandeze2971492009-10-24 04:23:03 +0000529}
530
531/// CreateFree - Generate the IR for a call to the builtin free function.
532/// Note: This function does not add the call to the basic block, that is the
533/// responsibility of the caller.
534Instruction* CallInst::CreateFree(Value* Source, BasicBlock *InsertAtEnd) {
535 Instruction* FreeCall = createFree(Source, NULL, InsertAtEnd);
536 assert(FreeCall && "CreateFree did not create a CallInst");
537 return FreeCall;
538}
539
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000540//===----------------------------------------------------------------------===//
541// InvokeInst Implementation
542//===----------------------------------------------------------------------===//
543
544void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000545 Value* const *Args, unsigned NumArgs) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000546 assert(NumOperands == 3+NumArgs && "NumOperands not set up?");
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000547 Op<-3>() = Fn;
548 Op<-2>() = IfNormal;
549 Op<-1>() = IfException;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000550 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000551 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000552 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000553
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000554 assert(((NumArgs == FTy->getNumParams()) ||
555 (FTy->isVarArg() && NumArgs > FTy->getNumParams())) &&
Gabor Greif668d7002010-03-23 13:45:54 +0000556 "Invoking a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000557
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000558 Use *OL = OperandList;
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000559 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000560 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000561 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000562 "Invoking a function with a bad signature!");
563
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000564 OL[i] = Args[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000565 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000566}
567
Misha Brukmanb1c93172005-04-21 23:48:37 +0000568InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000569 : TerminatorInst(II.getType(), Instruction::Invoke,
Gabor Greif697e94c2008-05-15 10:04:30 +0000570 OperandTraits<InvokeInst>::op_end(this)
571 - II.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000572 II.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000573 setAttributes(II.getAttributes());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000574 setCallingConv(II.getCallingConv());
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000575 Use *OL = OperandList, *InOL = II.OperandList;
576 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000577 OL[i] = InOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000578 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000579}
580
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000581BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
582 return getSuccessor(idx);
583}
584unsigned InvokeInst::getNumSuccessorsV() const {
585 return getNumSuccessors();
586}
587void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
588 return setSuccessor(idx, B);
589}
590
Devang Patelba3fa6c2008-09-23 23:03:40 +0000591bool InvokeInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000592 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000593 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000594 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000595 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000596 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000597}
598
Devang Patel4c758ea2008-09-25 21:00:45 +0000599void InvokeInst::addAttribute(unsigned i, Attributes attr) {
600 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000601 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000602 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000603}
604
Devang Patel4c758ea2008-09-25 21:00:45 +0000605void InvokeInst::removeAttribute(unsigned i, Attributes attr) {
606 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000607 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000608 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000609}
610
Duncan Sands5208d1a2007-11-28 17:07:01 +0000611
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000612//===----------------------------------------------------------------------===//
613// ReturnInst Implementation
614//===----------------------------------------------------------------------===//
615
Chris Lattner2195fc42007-02-24 00:55:48 +0000616ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000617 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000618 OperandTraits<ReturnInst>::op_end(this) -
619 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000620 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000621 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000622 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000623 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000624}
625
Owen Anderson55f1c092009-08-13 21:58:54 +0000626ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
627 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000628 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
629 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000630 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000631 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000632}
Owen Anderson55f1c092009-08-13 21:58:54 +0000633ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
634 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000635 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
636 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000637 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000638 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000639}
Owen Anderson55f1c092009-08-13 21:58:54 +0000640ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
641 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000642 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000643}
644
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000645unsigned ReturnInst::getNumSuccessorsV() const {
646 return getNumSuccessors();
647}
648
Devang Patelae682fb2008-02-26 17:56:20 +0000649/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
650/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000651void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000652 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000653}
654
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000655BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000656 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000657 return 0;
658}
659
Devang Patel59643e52008-02-23 00:35:18 +0000660ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000661}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000662
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000663//===----------------------------------------------------------------------===//
664// UnwindInst Implementation
665//===----------------------------------------------------------------------===//
666
Owen Anderson55f1c092009-08-13 21:58:54 +0000667UnwindInst::UnwindInst(LLVMContext &Context, Instruction *InsertBefore)
668 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
669 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000670}
Owen Anderson55f1c092009-08-13 21:58:54 +0000671UnwindInst::UnwindInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
672 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
673 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000674}
675
676
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000677unsigned UnwindInst::getNumSuccessorsV() const {
678 return getNumSuccessors();
679}
680
681void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000682 llvm_unreachable("UnwindInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000683}
684
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000685BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000686 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000687 return 0;
688}
689
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000690//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000691// UnreachableInst Implementation
692//===----------------------------------------------------------------------===//
693
Owen Anderson55f1c092009-08-13 21:58:54 +0000694UnreachableInst::UnreachableInst(LLVMContext &Context,
695 Instruction *InsertBefore)
696 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
697 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000698}
Owen Anderson55f1c092009-08-13 21:58:54 +0000699UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
700 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
701 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000702}
703
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000704unsigned UnreachableInst::getNumSuccessorsV() const {
705 return getNumSuccessors();
706}
707
708void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000709 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000710}
711
712BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000713 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000714 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000715}
716
717//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000718// BranchInst Implementation
719//===----------------------------------------------------------------------===//
720
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000721void BranchInst::AssertOK() {
722 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +0000723 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000724 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000725}
726
Chris Lattner2195fc42007-02-24 00:55:48 +0000727BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000728 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000729 OperandTraits<BranchInst>::op_end(this) - 1,
730 1, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000731 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000732 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000733}
734BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
735 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000736 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000737 OperandTraits<BranchInst>::op_end(this) - 3,
738 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000739 Op<-1>() = IfTrue;
740 Op<-2>() = IfFalse;
741 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000742#ifndef NDEBUG
743 AssertOK();
744#endif
745}
746
747BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000748 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000749 OperandTraits<BranchInst>::op_end(this) - 1,
750 1, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000751 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000752 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000753}
754
755BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
756 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000757 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000758 OperandTraits<BranchInst>::op_end(this) - 3,
759 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000760 Op<-1>() = IfTrue;
761 Op<-2>() = IfFalse;
762 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000763#ifndef NDEBUG
764 AssertOK();
765#endif
766}
767
768
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000769BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +0000770 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000771 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
772 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000773 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000774 if (BI.getNumOperands() != 1) {
775 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000776 Op<-3>() = BI.Op<-3>();
777 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000778 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000779 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000780}
781
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000782
783Use* Use::getPrefix() {
784 PointerIntPair<Use**, 2, PrevPtrTag> &PotentialPrefix(this[-1].Prev);
785 if (PotentialPrefix.getOpaqueValue())
786 return 0;
787
788 return reinterpret_cast<Use*>((char*)&PotentialPrefix + 1);
789}
790
791BranchInst::~BranchInst() {
792 if (NumOperands == 1) {
793 if (Use *Prefix = OperandList->getPrefix()) {
794 Op<-1>() = 0;
795 //
796 // mark OperandList to have a special value for scrutiny
797 // by baseclass destructors and operator delete
798 OperandList = Prefix;
799 } else {
800 NumOperands = 3;
801 OperandList = op_begin();
802 }
803 }
804}
805
806
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000807BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
808 return getSuccessor(idx);
809}
810unsigned BranchInst::getNumSuccessorsV() const {
811 return getNumSuccessors();
812}
813void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
814 setSuccessor(idx, B);
815}
816
817
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000818//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +0000819// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000820//===----------------------------------------------------------------------===//
821
Owen Andersonb6b25302009-07-14 23:09:55 +0000822static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000823 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +0000824 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000825 else {
826 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000827 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +0000828 assert(Amt->getType()->isIntegerTy() &&
829 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000830 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000831 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000832}
833
Victor Hernandez8acf2952009-10-23 21:09:37 +0000834AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize,
835 const Twine &Name, Instruction *InsertBefore)
836 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
837 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
838 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000839 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000840 setName(Name);
841}
842
843AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize,
844 const Twine &Name, BasicBlock *InsertAtEnd)
845 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
846 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
847 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000848 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000849 setName(Name);
850}
851
852AllocaInst::AllocaInst(const Type *Ty, const Twine &Name,
853 Instruction *InsertBefore)
854 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
855 getAISize(Ty->getContext(), 0), InsertBefore) {
856 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000857 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000858 setName(Name);
859}
860
861AllocaInst::AllocaInst(const Type *Ty, const Twine &Name,
862 BasicBlock *InsertAtEnd)
863 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
864 getAISize(Ty->getContext(), 0), InsertAtEnd) {
865 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000866 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000867 setName(Name);
868}
869
870AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
871 const Twine &Name, Instruction *InsertBefore)
872 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000873 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000874 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000875 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000876 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000877}
878
Victor Hernandez8acf2952009-10-23 21:09:37 +0000879AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
880 const Twine &Name, BasicBlock *InsertAtEnd)
881 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000882 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000883 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000884 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000885 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000886}
887
Gordon Henriksen14a55692007-12-10 02:14:30 +0000888// Out of line virtual method, so the vtable, etc has a home.
Victor Hernandez8acf2952009-10-23 21:09:37 +0000889AllocaInst::~AllocaInst() {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000890}
891
Victor Hernandez8acf2952009-10-23 21:09:37 +0000892void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000893 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +0000894 assert(Align <= MaximumAlignment &&
895 "Alignment is greater than MaximumAlignment!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +0000896 setInstructionSubclassData(Log2_32(Align) + 1);
Dan Gohmanaa583d72008-03-24 16:55:58 +0000897 assert(getAlignment() == Align && "Alignment representation error!");
898}
899
Victor Hernandez8acf2952009-10-23 21:09:37 +0000900bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000901 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
Dan Gohman9a1b8592010-09-27 15:15:44 +0000902 return !CI->isOne();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000903 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000904}
905
Victor Hernandez8acf2952009-10-23 21:09:37 +0000906const Type *AllocaInst::getAllocatedType() const {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000907 return getType()->getElementType();
908}
909
Chris Lattner8b291e62008-11-26 02:54:17 +0000910/// isStaticAlloca - Return true if this alloca is in the entry block of the
911/// function and is a constant size. If so, the code generator will fold it
912/// into the prolog/epilog code, so it is basically free.
913bool AllocaInst::isStaticAlloca() const {
914 // Must be constant size.
915 if (!isa<ConstantInt>(getArraySize())) return false;
916
917 // Must be in the entry block.
918 const BasicBlock *Parent = getParent();
919 return Parent == &Parent->getParent()->front();
920}
921
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000922//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000923// LoadInst Implementation
924//===----------------------------------------------------------------------===//
925
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000926void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +0000927 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000928 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000929}
930
Daniel Dunbar4975db62009-07-25 04:41:11 +0000931LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000932 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000933 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000934 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000935 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000936 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000937 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000938}
939
Daniel Dunbar4975db62009-07-25 04:41:11 +0000940LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000941 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000942 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000943 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000944 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000945 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000946 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000947}
948
Daniel Dunbar4975db62009-07-25 04:41:11 +0000949LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000950 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000951 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000952 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000953 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000954 setAlignment(0);
955 AssertOK();
956 setName(Name);
957}
958
Daniel Dunbar4975db62009-07-25 04:41:11 +0000959LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +0000960 unsigned Align, Instruction *InsertBef)
961 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
962 Load, Ptr, InsertBef) {
963 setVolatile(isVolatile);
964 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000965 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000966 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000967}
968
Daniel Dunbar4975db62009-07-25 04:41:11 +0000969LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000970 unsigned Align, BasicBlock *InsertAE)
971 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
972 Load, Ptr, InsertAE) {
973 setVolatile(isVolatile);
974 setAlignment(Align);
975 AssertOK();
976 setName(Name);
977}
978
Daniel Dunbar4975db62009-07-25 04:41:11 +0000979LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000980 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000981 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000982 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000983 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000984 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000985 AssertOK();
986 setName(Name);
987}
988
Daniel Dunbar27096822009-08-11 18:11:15 +0000989
990
991LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
992 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
993 Load, Ptr, InsertBef) {
994 setVolatile(false);
995 setAlignment(0);
996 AssertOK();
997 if (Name && Name[0]) setName(Name);
998}
999
1000LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
1001 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1002 Load, Ptr, InsertAE) {
1003 setVolatile(false);
1004 setAlignment(0);
1005 AssertOK();
1006 if (Name && Name[0]) setName(Name);
1007}
1008
1009LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1010 Instruction *InsertBef)
1011: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1012 Load, Ptr, InsertBef) {
1013 setVolatile(isVolatile);
1014 setAlignment(0);
1015 AssertOK();
1016 if (Name && Name[0]) setName(Name);
1017}
1018
1019LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1020 BasicBlock *InsertAE)
1021 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1022 Load, Ptr, InsertAE) {
1023 setVolatile(isVolatile);
1024 setAlignment(0);
1025 AssertOK();
1026 if (Name && Name[0]) setName(Name);
1027}
1028
Christopher Lamb84485702007-04-22 19:24:39 +00001029void LoadInst::setAlignment(unsigned Align) {
1030 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001031 assert(Align <= MaximumAlignment &&
1032 "Alignment is greater than MaximumAlignment!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001033 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
1034 ((Log2_32(Align)+1)<<1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001035 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001036}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001037
1038//===----------------------------------------------------------------------===//
1039// StoreInst Implementation
1040//===----------------------------------------------------------------------===//
1041
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001042void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001043 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001044 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001045 "Ptr must have pointer type!");
1046 assert(getOperand(0)->getType() ==
1047 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001048 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001049}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001050
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001051
1052StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001053 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001054 OperandTraits<StoreInst>::op_begin(this),
1055 OperandTraits<StoreInst>::operands(this),
1056 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001057 Op<0>() = val;
1058 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001059 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001060 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001061 AssertOK();
1062}
1063
1064StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001065 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001066 OperandTraits<StoreInst>::op_begin(this),
1067 OperandTraits<StoreInst>::operands(this),
1068 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001069 Op<0>() = val;
1070 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001071 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001072 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001073 AssertOK();
1074}
1075
Misha Brukmanb1c93172005-04-21 23:48:37 +00001076StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001077 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001078 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001079 OperandTraits<StoreInst>::op_begin(this),
1080 OperandTraits<StoreInst>::operands(this),
1081 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001082 Op<0>() = val;
1083 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001084 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001085 setAlignment(0);
1086 AssertOK();
1087}
1088
1089StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1090 unsigned Align, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001091 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001092 OperandTraits<StoreInst>::op_begin(this),
1093 OperandTraits<StoreInst>::operands(this),
1094 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001095 Op<0>() = val;
1096 Op<1>() = addr;
Christopher Lamb84485702007-04-22 19:24:39 +00001097 setVolatile(isVolatile);
1098 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001099 AssertOK();
1100}
1101
Misha Brukmanb1c93172005-04-21 23:48:37 +00001102StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001103 unsigned Align, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001104 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001105 OperandTraits<StoreInst>::op_begin(this),
1106 OperandTraits<StoreInst>::operands(this),
1107 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001108 Op<0>() = val;
1109 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001110 setVolatile(isVolatile);
1111 setAlignment(Align);
1112 AssertOK();
1113}
1114
1115StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001116 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001117 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001118 OperandTraits<StoreInst>::op_begin(this),
1119 OperandTraits<StoreInst>::operands(this),
1120 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001121 Op<0>() = val;
1122 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001123 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001124 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001125 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001126}
1127
Christopher Lamb84485702007-04-22 19:24:39 +00001128void StoreInst::setAlignment(unsigned Align) {
1129 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001130 assert(Align <= MaximumAlignment &&
1131 "Alignment is greater than MaximumAlignment!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001132 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
1133 ((Log2_32(Align)+1) << 1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001134 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001135}
1136
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001137//===----------------------------------------------------------------------===//
1138// GetElementPtrInst Implementation
1139//===----------------------------------------------------------------------===//
1140
Christopher Lambedf07882007-12-17 01:12:55 +00001141static unsigned retrieveAddrSpace(const Value *Val) {
1142 return cast<PointerType>(Val->getType())->getAddressSpace();
1143}
1144
Gabor Greif21ba1842008-06-06 20:28:12 +00001145void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001146 const Twine &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001147 assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
1148 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001149 OL[0] = Ptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001150
Chris Lattner79807c3d2007-01-31 19:47:18 +00001151 for (unsigned i = 0; i != NumIdx; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001152 OL[i+1] = Idx[i];
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001153
1154 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001155}
1156
Daniel Dunbar4975db62009-07-25 04:41:11 +00001157void GetElementPtrInst::init(Value *Ptr, Value *Idx, const Twine &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001158 assert(NumOperands == 2 && "NumOperands not initialized?");
1159 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001160 OL[0] = Ptr;
1161 OL[1] = Idx;
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001162
1163 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001164}
1165
Gabor Greiff6caff662008-05-10 08:32:32 +00001166GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
Gabor Greife9408e62008-05-27 11:03:29 +00001167 : Instruction(GEPI.getType(), GetElementPtr,
Gabor Greif697e94c2008-05-15 10:04:30 +00001168 OperandTraits<GetElementPtrInst>::op_end(this)
1169 - GEPI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001170 GEPI.getNumOperands()) {
1171 Use *OL = OperandList;
1172 Use *GEPIOL = GEPI.OperandList;
1173 for (unsigned i = 0, E = NumOperands; i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001174 OL[i] = GEPIOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001175 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001176}
1177
Chris Lattner82981202005-05-03 05:43:30 +00001178GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001179 const Twine &Name, Instruction *InBe)
Owen Anderson4056ca92009-07-29 22:17:13 +00001180 : Instruction(PointerType::get(
Owen Andersond420fd42009-07-16 00:03:07 +00001181 checkType(getIndexedType(Ptr->getType(),Idx)), retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001182 GetElementPtr,
1183 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1184 2, InBe) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001185 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001186}
1187
1188GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001189 const Twine &Name, BasicBlock *IAE)
Owen Anderson4056ca92009-07-29 22:17:13 +00001190 : Instruction(PointerType::get(
Owen Andersond420fd42009-07-16 00:03:07 +00001191 checkType(getIndexedType(Ptr->getType(),Idx)),
1192 retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001193 GetElementPtr,
1194 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1195 2, IAE) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001196 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001197}
1198
Chris Lattner6090a422009-03-09 04:46:40 +00001199/// getIndexedType - Returns the type of the element that would be accessed with
1200/// a gep instruction with the specified parameters.
1201///
1202/// The Idxs pointer should point to a continuous piece of memory containing the
1203/// indices, either as Value* or uint64_t.
1204///
1205/// A null type is returned if the indices are invalid for the specified
1206/// pointer type.
1207///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001208template <typename IndexTy>
Chris Lattner6090a422009-03-09 04:46:40 +00001209static const Type* getIndexedTypeInternal(const Type *Ptr, IndexTy const *Idxs,
1210 unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001211 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1212 if (!PTy) return 0; // Type isn't a pointer type!
1213 const Type *Agg = PTy->getElementType();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001214
Chris Lattner6090a422009-03-09 04:46:40 +00001215 // Handle the special case of the empty set index set, which is always valid.
Dan Gohman12fce772008-05-15 19:50:34 +00001216 if (NumIdx == 0)
1217 return Agg;
Chris Lattner6090a422009-03-09 04:46:40 +00001218
1219 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattner4a488152009-03-09 04:56:22 +00001220 // it cannot be 'stepped over'. We explicitly allow abstract types (those
1221 // that contain opaque types) under the assumption that it will be resolved to
1222 // a sane type later.
1223 if (!Agg->isSized() && !Agg->isAbstract())
Chris Lattner6090a422009-03-09 04:46:40 +00001224 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001225
Dan Gohman1ecaf452008-05-31 00:58:22 +00001226 unsigned CurIdx = 1;
1227 for (; CurIdx != NumIdx; ++CurIdx) {
1228 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
Duncan Sands19d0b472010-02-16 11:11:14 +00001229 if (!CT || CT->isPointerTy()) return 0;
Matthijs Kooijman04468622008-07-29 08:46:11 +00001230 IndexTy Index = Idxs[CurIdx];
Dan Gohman1ecaf452008-05-31 00:58:22 +00001231 if (!CT->indexValid(Index)) return 0;
1232 Agg = CT->getTypeAtIndex(Index);
1233
1234 // If the new type forwards to another type, then it is in the middle
1235 // of being refined to another type (and hence, may have dropped all
1236 // references to what it was using before). So, use the new forwarded
1237 // type.
1238 if (const Type *Ty = Agg->getForwardedType())
1239 Agg = Ty;
1240 }
1241 return CurIdx == NumIdx ? Agg : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001242}
1243
Matthijs Kooijman04468622008-07-29 08:46:11 +00001244const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1245 Value* const *Idxs,
1246 unsigned NumIdx) {
1247 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1248}
1249
1250const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1251 uint64_t const *Idxs,
1252 unsigned NumIdx) {
1253 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1254}
1255
Chris Lattner82981202005-05-03 05:43:30 +00001256const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1257 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1258 if (!PTy) return 0; // Type isn't a pointer type!
1259
1260 // Check the pointer index.
1261 if (!PTy->indexValid(Idx)) return 0;
1262
Chris Lattnerc2233332005-05-03 16:44:45 +00001263 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +00001264}
1265
Chris Lattner45f15572007-04-14 00:12:57 +00001266
1267/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1268/// zeros. If so, the result pointer and the first operand have the same
1269/// value, just potentially different types.
1270bool GetElementPtrInst::hasAllZeroIndices() const {
1271 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1272 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1273 if (!CI->isZero()) return false;
1274 } else {
1275 return false;
1276 }
1277 }
1278 return true;
1279}
1280
Chris Lattner27058292007-04-27 20:35:56 +00001281/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1282/// constant integers. If so, the result pointer and the first operand have
1283/// a constant offset between them.
1284bool GetElementPtrInst::hasAllConstantIndices() const {
1285 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1286 if (!isa<ConstantInt>(getOperand(i)))
1287 return false;
1288 }
1289 return true;
1290}
1291
Dan Gohman1b849082009-09-07 23:54:19 +00001292void GetElementPtrInst::setIsInBounds(bool B) {
1293 cast<GEPOperator>(this)->setIsInBounds(B);
1294}
Chris Lattner45f15572007-04-14 00:12:57 +00001295
Nick Lewycky28a5f252009-09-27 21:33:04 +00001296bool GetElementPtrInst::isInBounds() const {
1297 return cast<GEPOperator>(this)->isInBounds();
1298}
1299
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001300//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001301// ExtractElementInst Implementation
1302//===----------------------------------------------------------------------===//
1303
1304ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001305 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001306 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001307 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001308 ExtractElement,
1309 OperandTraits<ExtractElementInst>::op_begin(this),
1310 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001311 assert(isValidOperands(Val, Index) &&
1312 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001313 Op<0>() = Val;
1314 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001315 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001316}
1317
1318ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001319 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001320 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001321 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001322 ExtractElement,
1323 OperandTraits<ExtractElementInst>::op_begin(this),
1324 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001325 assert(isValidOperands(Val, Index) &&
1326 "Invalid extractelement instruction operands!");
1327
Gabor Greif2d3024d2008-05-26 21:33:52 +00001328 Op<0>() = Val;
1329 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001330 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001331}
1332
Chris Lattner65511ff2006-10-05 06:24:58 +00001333
Chris Lattner54865b32006-04-08 04:05:48 +00001334bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001335 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy(32))
Chris Lattner54865b32006-04-08 04:05:48 +00001336 return false;
1337 return true;
1338}
1339
1340
Robert Bocchino23004482006-01-10 19:05:34 +00001341//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001342// InsertElementInst Implementation
1343//===----------------------------------------------------------------------===//
1344
Chris Lattner54865b32006-04-08 04:05:48 +00001345InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001346 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001347 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001348 : Instruction(Vec->getType(), InsertElement,
1349 OperandTraits<InsertElementInst>::op_begin(this),
1350 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001351 assert(isValidOperands(Vec, Elt, Index) &&
1352 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001353 Op<0>() = Vec;
1354 Op<1>() = Elt;
1355 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001356 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001357}
1358
Chris Lattner54865b32006-04-08 04:05:48 +00001359InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001360 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001361 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001362 : Instruction(Vec->getType(), InsertElement,
1363 OperandTraits<InsertElementInst>::op_begin(this),
1364 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001365 assert(isValidOperands(Vec, Elt, Index) &&
1366 "Invalid insertelement instruction operands!");
1367
Gabor Greif2d3024d2008-05-26 21:33:52 +00001368 Op<0>() = Vec;
1369 Op<1>() = Elt;
1370 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001371 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001372}
1373
Chris Lattner54865b32006-04-08 04:05:48 +00001374bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1375 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001376 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001377 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001378
Reid Spencerd84d35b2007-02-15 02:26:10 +00001379 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001380 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001381
Duncan Sands9dff9be2010-02-15 16:12:20 +00001382 if (!Index->getType()->isIntegerTy(32))
Dan Gohman4fe64de2009-06-14 23:30:43 +00001383 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001384 return true;
1385}
1386
1387
Robert Bocchinoca27f032006-01-17 20:07:22 +00001388//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001389// ShuffleVectorInst Implementation
1390//===----------------------------------------------------------------------===//
1391
1392ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001393 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001394 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001395: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001396 cast<VectorType>(Mask->getType())->getNumElements()),
1397 ShuffleVector,
1398 OperandTraits<ShuffleVectorInst>::op_begin(this),
1399 OperandTraits<ShuffleVectorInst>::operands(this),
1400 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001401 assert(isValidOperands(V1, V2, Mask) &&
1402 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001403 Op<0>() = V1;
1404 Op<1>() = V2;
1405 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001406 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001407}
1408
1409ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001410 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001411 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001412: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1413 cast<VectorType>(Mask->getType())->getNumElements()),
1414 ShuffleVector,
1415 OperandTraits<ShuffleVectorInst>::op_begin(this),
1416 OperandTraits<ShuffleVectorInst>::operands(this),
1417 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001418 assert(isValidOperands(V1, V2, Mask) &&
1419 "Invalid shuffle vector instruction operands!");
1420
Gabor Greif2d3024d2008-05-26 21:33:52 +00001421 Op<0>() = V1;
1422 Op<1>() = V2;
1423 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001424 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001425}
1426
Mon P Wang25f01062008-11-10 04:46:22 +00001427bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001428 const Value *Mask) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001429 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001430 return false;
1431
1432 const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
Nate Begeman60a31c32010-08-13 00:16:46 +00001433 if (MaskTy == 0 || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001434 return false;
Nate Begeman60a31c32010-08-13 00:16:46 +00001435
1436 // Check to see if Mask is valid.
1437 if (const ConstantVector *MV = dyn_cast<ConstantVector>(Mask)) {
1438 const VectorType *VTy = cast<VectorType>(V1->getType());
1439 for (unsigned i = 0, e = MV->getNumOperands(); i != e; ++i) {
1440 if (ConstantInt* CI = dyn_cast<ConstantInt>(MV->getOperand(i))) {
1441 if (CI->uge(VTy->getNumElements()*2))
1442 return false;
1443 } else if (!isa<UndefValue>(MV->getOperand(i))) {
1444 return false;
1445 }
1446 }
1447 }
1448 else if (!isa<UndefValue>(Mask) && !isa<ConstantAggregateZero>(Mask))
1449 return false;
1450
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001451 return true;
1452}
1453
Chris Lattnerf724e342008-03-02 05:28:33 +00001454/// getMaskValue - Return the index from the shuffle mask for the specified
1455/// output result. This is either -1 if the element is undef or a number less
1456/// than 2*numelements.
1457int ShuffleVectorInst::getMaskValue(unsigned i) const {
1458 const Constant *Mask = cast<Constant>(getOperand(2));
1459 if (isa<UndefValue>(Mask)) return -1;
1460 if (isa<ConstantAggregateZero>(Mask)) return 0;
1461 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1462 assert(i < MaskCV->getNumOperands() && "Index out of range");
1463
1464 if (isa<UndefValue>(MaskCV->getOperand(i)))
1465 return -1;
1466 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1467}
1468
Dan Gohman12fce772008-05-15 19:50:34 +00001469//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001470// InsertValueInst Class
1471//===----------------------------------------------------------------------===//
1472
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001473void InsertValueInst::init(Value *Agg, Value *Val, const unsigned *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001474 unsigned NumIdx, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001475 assert(NumOperands == 2 && "NumOperands not initialized?");
1476 Op<0>() = Agg;
1477 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001478
Dan Gohmandd41bba2010-06-21 19:47:52 +00001479 Indices.append(Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001480 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001481}
1482
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001483void InsertValueInst::init(Value *Agg, Value *Val, unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001484 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001485 assert(NumOperands == 2 && "NumOperands not initialized?");
1486 Op<0>() = Agg;
1487 Op<1>() = Val;
1488
1489 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001490 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001491}
1492
1493InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001494 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001495 OperandTraits<InsertValueInst>::op_begin(this), 2),
1496 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001497 Op<0>() = IVI.getOperand(0);
1498 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001499 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001500}
1501
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001502InsertValueInst::InsertValueInst(Value *Agg,
1503 Value *Val,
1504 unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001505 const Twine &Name,
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001506 Instruction *InsertBefore)
1507 : Instruction(Agg->getType(), InsertValue,
1508 OperandTraits<InsertValueInst>::op_begin(this),
1509 2, InsertBefore) {
1510 init(Agg, Val, Idx, Name);
1511}
1512
1513InsertValueInst::InsertValueInst(Value *Agg,
1514 Value *Val,
1515 unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001516 const Twine &Name,
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001517 BasicBlock *InsertAtEnd)
1518 : Instruction(Agg->getType(), InsertValue,
1519 OperandTraits<InsertValueInst>::op_begin(this),
1520 2, InsertAtEnd) {
1521 init(Agg, Val, Idx, Name);
1522}
1523
Dan Gohman0752bff2008-05-23 00:36:11 +00001524//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001525// ExtractValueInst Class
1526//===----------------------------------------------------------------------===//
1527
Gabor Greifcbcc4952008-06-06 21:06:32 +00001528void ExtractValueInst::init(const unsigned *Idx, unsigned NumIdx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001529 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001530 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001531
Dan Gohmandd41bba2010-06-21 19:47:52 +00001532 Indices.append(Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001533 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001534}
1535
Daniel Dunbar4975db62009-07-25 04:41:11 +00001536void ExtractValueInst::init(unsigned Idx, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001537 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001538
1539 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001540 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001541}
1542
1543ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001544 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001545 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001546 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001547}
1548
Dan Gohman12fce772008-05-15 19:50:34 +00001549// getIndexedType - Returns the type of the element that would be extracted
1550// with an extractvalue instruction with the specified parameters.
1551//
1552// A null type is returned if the indices are invalid for the specified
1553// pointer type.
1554//
1555const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001556 const unsigned *Idxs,
Dan Gohman12fce772008-05-15 19:50:34 +00001557 unsigned NumIdx) {
1558 unsigned CurIdx = 0;
1559 for (; CurIdx != NumIdx; ++CurIdx) {
1560 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
Duncan Sands19d0b472010-02-16 11:11:14 +00001561 if (!CT || CT->isPointerTy() || CT->isVectorTy()) return 0;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001562 unsigned Index = Idxs[CurIdx];
Dan Gohman12fce772008-05-15 19:50:34 +00001563 if (!CT->indexValid(Index)) return 0;
1564 Agg = CT->getTypeAtIndex(Index);
1565
1566 // If the new type forwards to another type, then it is in the middle
1567 // of being refined to another type (and hence, may have dropped all
1568 // references to what it was using before). So, use the new forwarded
1569 // type.
1570 if (const Type *Ty = Agg->getForwardedType())
1571 Agg = Ty;
1572 }
1573 return CurIdx == NumIdx ? Agg : 0;
1574}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001575
Dan Gohman4a3125b2008-06-17 21:07:55 +00001576const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohmand87e8132008-06-20 00:47:44 +00001577 unsigned Idx) {
1578 return getIndexedType(Agg, &Idx, 1);
Dan Gohman4a3125b2008-06-17 21:07:55 +00001579}
1580
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001581//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001582// BinaryOperator Class
1583//===----------------------------------------------------------------------===//
1584
Chris Lattner2195fc42007-02-24 00:55:48 +00001585BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001586 const Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001587 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001588 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001589 OperandTraits<BinaryOperator>::op_begin(this),
1590 OperandTraits<BinaryOperator>::operands(this),
1591 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001592 Op<0>() = S1;
1593 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001594 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001595 setName(Name);
1596}
1597
1598BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001599 const Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001600 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001601 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001602 OperandTraits<BinaryOperator>::op_begin(this),
1603 OperandTraits<BinaryOperator>::operands(this),
1604 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001605 Op<0>() = S1;
1606 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001607 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001608 setName(Name);
1609}
1610
1611
1612void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001613 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001614 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001615 assert(LHS->getType() == RHS->getType() &&
1616 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001617#ifndef NDEBUG
1618 switch (iType) {
1619 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001620 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001621 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001622 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001623 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001624 "Tried to create an integer operation on a non-integer type!");
1625 break;
1626 case FAdd: case FSub:
1627 case FMul:
1628 assert(getType() == LHS->getType() &&
1629 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001630 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001631 "Tried to create a floating-point operation on a "
1632 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001633 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001634 case UDiv:
1635 case SDiv:
1636 assert(getType() == LHS->getType() &&
1637 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001638 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001639 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001640 "Incorrect operand type (not integer) for S/UDIV");
1641 break;
1642 case FDiv:
1643 assert(getType() == LHS->getType() &&
1644 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001645 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001646 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001647 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001648 case URem:
1649 case SRem:
1650 assert(getType() == LHS->getType() &&
1651 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001652 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001653 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001654 "Incorrect operand type (not integer) for S/UREM");
1655 break;
1656 case FRem:
1657 assert(getType() == LHS->getType() &&
1658 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001659 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001660 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001661 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001662 case Shl:
1663 case LShr:
1664 case AShr:
1665 assert(getType() == LHS->getType() &&
1666 "Shift operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001667 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001668 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001669 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001670 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001671 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001672 case And: case Or:
1673 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001674 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001675 "Logical operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001676 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001677 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001678 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001679 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001680 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001681 default:
1682 break;
1683 }
1684#endif
1685}
1686
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001687BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001688 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001689 Instruction *InsertBefore) {
1690 assert(S1->getType() == S2->getType() &&
1691 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001692 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001693}
1694
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001695BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001696 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001697 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001698 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001699 InsertAtEnd->getInstList().push_back(Res);
1700 return Res;
1701}
1702
Dan Gohman5476cfd2009-08-12 16:23:25 +00001703BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001704 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001705 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001706 return new BinaryOperator(Instruction::Sub,
1707 zero, Op,
1708 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001709}
1710
Dan Gohman5476cfd2009-08-12 16:23:25 +00001711BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001712 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001713 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001714 return new BinaryOperator(Instruction::Sub,
1715 zero, Op,
1716 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001717}
1718
Dan Gohman4ab44202009-12-18 02:58:50 +00001719BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1720 Instruction *InsertBefore) {
1721 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1722 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
1723}
1724
1725BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1726 BasicBlock *InsertAtEnd) {
1727 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1728 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
1729}
1730
Duncan Sandsfa5f5962010-02-02 12:53:04 +00001731BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1732 Instruction *InsertBefore) {
1733 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1734 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
1735}
1736
1737BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1738 BasicBlock *InsertAtEnd) {
1739 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1740 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
1741}
1742
Dan Gohman5476cfd2009-08-12 16:23:25 +00001743BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001744 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001745 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001746 return new BinaryOperator(Instruction::FSub,
1747 zero, Op,
1748 Op->getType(), Name, InsertBefore);
1749}
1750
Dan Gohman5476cfd2009-08-12 16:23:25 +00001751BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001752 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001753 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001754 return new BinaryOperator(Instruction::FSub,
1755 zero, Op,
1756 Op->getType(), Name, InsertAtEnd);
1757}
1758
Dan Gohman5476cfd2009-08-12 16:23:25 +00001759BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001760 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001761 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001762 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001763 C = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001764 C = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001765 std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001766 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001767 C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001768 }
1769
1770 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001771 Op->getType(), Name, InsertBefore);
1772}
1773
Dan Gohman5476cfd2009-08-12 16:23:25 +00001774BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001775 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001776 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001777 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001778 // Create a vector of all ones values.
Owen Anderson5a1acd92009-07-31 20:28:14 +00001779 Constant *Elt = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001780 AllOnes = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001781 std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001782 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001783 AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001784 }
1785
1786 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001787 Op->getType(), Name, InsertAtEnd);
1788}
1789
1790
1791// isConstantAllOnes - Helper function for several functions below
1792static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001793 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1794 return CI->isAllOnesValue();
1795 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1796 return CV->isAllOnesValue();
1797 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001798}
1799
Owen Andersonbb2501b2009-07-13 22:18:28 +00001800bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001801 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1802 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001803 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1804 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001805 return false;
1806}
1807
Owen Andersonbb2501b2009-07-13 22:18:28 +00001808bool BinaryOperator::isFNeg(const Value *V) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001809 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1810 if (Bop->getOpcode() == Instruction::FSub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001811 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
Dan Gohman11ff5702009-09-11 00:05:10 +00001812 return C->isNegativeZeroValue();
Dan Gohmana5b96452009-06-04 22:49:04 +00001813 return false;
1814}
1815
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001816bool BinaryOperator::isNot(const Value *V) {
1817 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1818 return (Bop->getOpcode() == Instruction::Xor &&
1819 (isConstantAllOnes(Bop->getOperand(1)) ||
1820 isConstantAllOnes(Bop->getOperand(0))));
1821 return false;
1822}
1823
Chris Lattner2c7d1772005-04-24 07:28:37 +00001824Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00001825 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001826}
1827
Chris Lattner2c7d1772005-04-24 07:28:37 +00001828const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1829 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001830}
1831
Dan Gohmana5b96452009-06-04 22:49:04 +00001832Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001833 return cast<BinaryOperator>(BinOp)->getOperand(1);
1834}
1835
1836const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
1837 return getFNegArgument(const_cast<Value*>(BinOp));
1838}
1839
Chris Lattner2c7d1772005-04-24 07:28:37 +00001840Value *BinaryOperator::getNotArgument(Value *BinOp) {
1841 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1842 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1843 Value *Op0 = BO->getOperand(0);
1844 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001845 if (isConstantAllOnes(Op0)) return Op1;
1846
1847 assert(isConstantAllOnes(Op1));
1848 return Op0;
1849}
1850
Chris Lattner2c7d1772005-04-24 07:28:37 +00001851const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1852 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001853}
1854
1855
1856// swapOperands - Exchange the two operands to this instruction. This
1857// instruction is safe to use on any binary instruction and does not
1858// modify the semantics of the instruction. If the instruction is
1859// order dependent (SetLT f.e.) the opcode is changed.
1860//
1861bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001862 if (!isCommutative())
1863 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001864 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001865 return false;
1866}
1867
Dan Gohman1b849082009-09-07 23:54:19 +00001868void BinaryOperator::setHasNoUnsignedWrap(bool b) {
1869 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
1870}
1871
1872void BinaryOperator::setHasNoSignedWrap(bool b) {
1873 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
1874}
1875
1876void BinaryOperator::setIsExact(bool b) {
1877 cast<SDivOperator>(this)->setIsExact(b);
1878}
1879
Nick Lewycky28a5f252009-09-27 21:33:04 +00001880bool BinaryOperator::hasNoUnsignedWrap() const {
1881 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
1882}
1883
1884bool BinaryOperator::hasNoSignedWrap() const {
1885 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
1886}
1887
1888bool BinaryOperator::isExact() const {
1889 return cast<SDivOperator>(this)->isExact();
1890}
1891
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001892//===----------------------------------------------------------------------===//
1893// CastInst Class
1894//===----------------------------------------------------------------------===//
1895
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001896// Just determine if this cast only deals with integral->integral conversion.
1897bool CastInst::isIntegerCast() const {
1898 switch (getOpcode()) {
1899 default: return false;
1900 case Instruction::ZExt:
1901 case Instruction::SExt:
1902 case Instruction::Trunc:
1903 return true;
1904 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00001905 return getOperand(0)->getType()->isIntegerTy() &&
1906 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001907 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001908}
1909
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001910bool CastInst::isLosslessCast() const {
1911 // Only BitCast can be lossless, exit fast if we're not BitCast
1912 if (getOpcode() != Instruction::BitCast)
1913 return false;
1914
1915 // Identity cast is always lossless
1916 const Type* SrcTy = getOperand(0)->getType();
1917 const Type* DstTy = getType();
1918 if (SrcTy == DstTy)
1919 return true;
1920
Reid Spencer8d9336d2006-12-31 05:26:44 +00001921 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00001922 if (SrcTy->isPointerTy())
1923 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001924 return false; // Other types have no identity values
1925}
1926
1927/// This function determines if the CastInst does not require any bits to be
1928/// changed in order to effect the cast. Essentially, it identifies cases where
1929/// no code gen is necessary for the cast, hence the name no-op cast. For
1930/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00001931/// # bitcast i32* %x to i8*
1932/// # bitcast <2 x i32> %x to <4 x i16>
1933/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001934/// @brief Determine if the described cast is a no-op.
1935bool CastInst::isNoopCast(Instruction::CastOps Opcode,
1936 const Type *SrcTy,
1937 const Type *DestTy,
1938 const Type *IntPtrTy) {
1939 switch (Opcode) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001940 default:
1941 assert(!"Invalid CastOp");
1942 case Instruction::Trunc:
1943 case Instruction::ZExt:
1944 case Instruction::SExt:
1945 case Instruction::FPTrunc:
1946 case Instruction::FPExt:
1947 case Instruction::UIToFP:
1948 case Instruction::SIToFP:
1949 case Instruction::FPToUI:
1950 case Instruction::FPToSI:
1951 return false; // These always modify bits
1952 case Instruction::BitCast:
1953 return true; // BitCast never modifies bits.
1954 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001955 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001956 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001957 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001958 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001959 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001960 }
1961}
1962
Dan Gohman0d7f3b82010-05-28 21:41:37 +00001963/// @brief Determine if a cast is a no-op.
1964bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1965 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
1966}
1967
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001968/// This function determines if a pair of casts can be eliminated and what
1969/// opcode should be used in the elimination. This assumes that there are two
1970/// instructions like this:
1971/// * %F = firstOpcode SrcTy %x to MidTy
1972/// * %S = secondOpcode MidTy %F to DstTy
1973/// The function returns a resultOpcode so these two casts can be replaced with:
1974/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1975/// If no such cast is permited, the function returns 0.
1976unsigned CastInst::isEliminableCastPair(
1977 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1978 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1979{
1980 // Define the 144 possibilities for these two cast instructions. The values
1981 // in this matrix determine what to do in a given situation and select the
1982 // case in the switch below. The rows correspond to firstOp, the columns
1983 // correspond to secondOp. In looking at the table below, keep in mind
1984 // the following cast properties:
1985 //
1986 // Size Compare Source Destination
1987 // Operator Src ? Size Type Sign Type Sign
1988 // -------- ------------ ------------------- ---------------------
1989 // TRUNC > Integer Any Integral Any
1990 // ZEXT < Integral Unsigned Integer Any
1991 // SEXT < Integral Signed Integer Any
1992 // FPTOUI n/a FloatPt n/a Integral Unsigned
1993 // FPTOSI n/a FloatPt n/a Integral Signed
1994 // UITOFP n/a Integral Unsigned FloatPt n/a
1995 // SITOFP n/a Integral Signed FloatPt n/a
1996 // FPTRUNC > FloatPt n/a FloatPt n/a
1997 // FPEXT < FloatPt n/a FloatPt n/a
1998 // PTRTOINT n/a Pointer n/a Integral Unsigned
1999 // INTTOPTR n/a Integral Unsigned Pointer n/a
Dan Gohmaneb7111b2010-04-07 23:22:42 +00002000 // BITCAST = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00002001 //
2002 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00002003 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2004 // into "fptoui double to i64", but this loses information about the range
Chris Lattner6f6b4972006-12-05 23:43:59 +00002005 // of the produced value (we no longer know the top-part is all zeros).
2006 // Further this conversion is often much more expensive for typical hardware,
2007 // and causes issues when building libgcc. We disallow fptosi+sext for the
2008 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002009 const unsigned numCastOps =
2010 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2011 static const uint8_t CastResults[numCastOps][numCastOps] = {
2012 // T F F U S F F P I B -+
2013 // R Z S P P I I T P 2 N T |
2014 // U E E 2 2 2 2 R E I T C +- secondOp
2015 // N X X U S F F N X N 2 V |
2016 // C T T I I P P C T T P T -+
2017 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
2018 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
2019 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00002020 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
2021 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002022 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
2023 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
2024 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
2025 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
2026 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
2027 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
2028 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
2029 };
Chris Lattner25eea4d2010-07-12 01:19:22 +00002030
2031 // If either of the casts are a bitcast from scalar to vector, disallow the
2032 // merging.
2033 if ((firstOp == Instruction::BitCast &&
2034 isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2035 (secondOp == Instruction::BitCast &&
2036 isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2037 return 0; // Disallowed
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002038
2039 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2040 [secondOp-Instruction::CastOpsBegin];
2041 switch (ElimCase) {
2042 case 0:
2043 // categorically disallowed
2044 return 0;
2045 case 1:
2046 // allowed, use first cast's opcode
2047 return firstOp;
2048 case 2:
2049 // allowed, use second cast's opcode
2050 return secondOp;
2051 case 3:
2052 // no-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002053 // is integer and we are not converting between a vector and a
Chris Lattner531732b2010-01-23 04:42:42 +00002054 // non vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002055 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002056 return firstOp;
2057 return 0;
2058 case 4:
2059 // no-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002060 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002061 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002062 return firstOp;
2063 return 0;
2064 case 5:
2065 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002066 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002067 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002068 return secondOp;
2069 return 0;
2070 case 6:
2071 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002072 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002073 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002074 return secondOp;
2075 return 0;
2076 case 7: {
2077 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
Dan Gohman9413de12009-07-21 23:19:40 +00002078 if (!IntPtrTy)
2079 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002080 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2081 unsigned MidSize = MidTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002082 if (MidSize >= PtrSize)
2083 return Instruction::BitCast;
2084 return 0;
2085 }
2086 case 8: {
2087 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2088 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2089 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002090 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2091 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002092 if (SrcSize == DstSize)
2093 return Instruction::BitCast;
2094 else if (SrcSize < DstSize)
2095 return firstOp;
2096 return secondOp;
2097 }
2098 case 9: // zext, sext -> zext, because sext can't sign extend after zext
2099 return Instruction::ZExt;
2100 case 10:
2101 // fpext followed by ftrunc is allowed if the bit size returned to is
2102 // the same as the original, in which case its just a bitcast
2103 if (SrcTy == DstTy)
2104 return Instruction::BitCast;
2105 return 0; // If the types are not the same we can't eliminate it.
2106 case 11:
2107 // bitcast followed by ptrtoint is allowed as long as the bitcast
2108 // is a pointer to pointer cast.
Duncan Sands19d0b472010-02-16 11:11:14 +00002109 if (SrcTy->isPointerTy() && MidTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002110 return secondOp;
2111 return 0;
2112 case 12:
2113 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
Duncan Sands19d0b472010-02-16 11:11:14 +00002114 if (MidTy->isPointerTy() && DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002115 return firstOp;
2116 return 0;
2117 case 13: {
2118 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Dan Gohman9413de12009-07-21 23:19:40 +00002119 if (!IntPtrTy)
2120 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002121 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2122 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2123 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002124 if (SrcSize <= PtrSize && SrcSize == DstSize)
2125 return Instruction::BitCast;
2126 return 0;
2127 }
2128 case 99:
2129 // cast combination can't happen (error in input). This is for all cases
2130 // where the MidTy is not the same for the two cast instructions.
2131 assert(!"Invalid Cast Combination");
2132 return 0;
2133 default:
2134 assert(!"Error in CastResults table!!!");
2135 return 0;
2136 }
2137 return 0;
2138}
2139
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002140CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002141 const Twine &Name, Instruction *InsertBefore) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002142 // Construct and return the appropriate CastInst subclass
2143 switch (op) {
2144 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2145 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2146 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2147 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2148 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2149 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2150 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2151 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2152 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2153 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2154 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2155 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2156 default:
2157 assert(!"Invalid opcode provided");
2158 }
2159 return 0;
2160}
2161
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002162CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002163 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002164 // Construct and return the appropriate CastInst subclass
2165 switch (op) {
2166 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2167 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2168 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2169 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2170 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2171 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2172 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2173 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2174 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2175 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2176 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2177 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2178 default:
2179 assert(!"Invalid opcode provided");
2180 }
2181 return 0;
2182}
2183
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002184CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002185 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002186 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002187 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002188 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2189 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002190}
2191
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002192CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002193 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002194 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002195 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002196 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2197 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002198}
2199
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002200CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002201 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002202 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002203 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002204 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2205 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002206}
2207
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002208CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002209 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002210 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002211 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002212 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2213 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002214}
2215
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002216CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002217 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002218 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002219 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002220 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2221 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002222}
2223
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002224CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002225 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002226 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002227 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002228 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2229 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002230}
2231
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002232CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002233 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002234 BasicBlock *InsertAtEnd) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002235 assert(S->getType()->isPointerTy() && "Invalid cast");
2236 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002237 "Invalid cast");
2238
Duncan Sands9dff9be2010-02-15 16:12:20 +00002239 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002240 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2241 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002242}
2243
2244/// @brief Create a BitCast or a PtrToInt cast instruction
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002245CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002246 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002247 Instruction *InsertBefore) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002248 assert(S->getType()->isPointerTy() && "Invalid cast");
2249 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002250 "Invalid cast");
2251
Duncan Sands9dff9be2010-02-15 16:12:20 +00002252 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002253 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2254 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002255}
2256
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002257CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002258 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002259 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002260 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002261 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002262 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2263 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002264 Instruction::CastOps opcode =
2265 (SrcBits == DstBits ? Instruction::BitCast :
2266 (SrcBits > DstBits ? Instruction::Trunc :
2267 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002268 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002269}
2270
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002271CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002272 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002273 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002274 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002275 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002276 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2277 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002278 Instruction::CastOps opcode =
2279 (SrcBits == DstBits ? Instruction::BitCast :
2280 (SrcBits > DstBits ? Instruction::Trunc :
2281 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002282 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002283}
2284
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002285CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002286 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002287 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002288 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002289 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002290 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2291 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002292 Instruction::CastOps opcode =
2293 (SrcBits == DstBits ? Instruction::BitCast :
2294 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002295 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002296}
2297
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002298CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002299 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002300 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002301 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002302 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002303 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2304 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002305 Instruction::CastOps opcode =
2306 (SrcBits == DstBits ? Instruction::BitCast :
2307 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002308 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002309}
2310
Duncan Sands55e50902008-01-06 10:12:28 +00002311// Check whether it is valid to call getCastOpcode for these types.
2312// This routine must be kept in sync with getCastOpcode.
2313bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
2314 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2315 return false;
2316
2317 if (SrcTy == DestTy)
2318 return true;
2319
2320 // Get the bit sizes, we'll need these
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002321 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2322 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Duncan Sands55e50902008-01-06 10:12:28 +00002323
2324 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002325 if (DestTy->isIntegerTy()) { // Casting to integral
2326 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002327 return true;
Duncan Sands9dff9be2010-02-15 16:12:20 +00002328 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002329 return true;
2330 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002331 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002332 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002333 } else { // Casting from something else
Duncan Sands19d0b472010-02-16 11:11:14 +00002334 return SrcTy->isPointerTy();
Duncan Sands55e50902008-01-06 10:12:28 +00002335 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002336 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2337 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002338 return true;
Duncan Sands9dff9be2010-02-15 16:12:20 +00002339 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002340 return true;
2341 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002342 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002343 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002344 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002345 return false;
2346 }
2347 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002348 // Casting to vector
Duncan Sands55e50902008-01-06 10:12:28 +00002349 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002350 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002351 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002352 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002353 return DestPTy->getBitWidth() == SrcBits;
2354 }
Duncan Sands19d0b472010-02-16 11:11:14 +00002355 } else if (DestTy->isPointerTy()) { // Casting to pointer
2356 if (SrcTy->isPointerTy()) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002357 return true;
Duncan Sands9dff9be2010-02-15 16:12:20 +00002358 } else if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002359 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002360 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002361 return false;
2362 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002363 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002364 return false;
2365 }
2366}
2367
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002368// Provide a way to get a "cast" where the cast opcode is inferred from the
2369// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002370// logic in the castIsValid function below. This axiom should hold:
2371// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2372// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002373// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002374// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002375Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002376CastInst::getCastOpcode(
2377 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002378 // Get the bit sizes, we'll need these
2379 const Type *SrcTy = Src->getType();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002380 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2381 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002382
Duncan Sands55e50902008-01-06 10:12:28 +00002383 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2384 "Only first class types are castable!");
2385
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002386 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002387 if (DestTy->isIntegerTy()) { // Casting to integral
2388 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002389 if (DestBits < SrcBits)
2390 return Trunc; // int -> smaller int
2391 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002392 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002393 return SExt; // signed -> SEXT
2394 else
2395 return ZExt; // unsigned -> ZEXT
2396 } else {
2397 return BitCast; // Same size, No-op cast
2398 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002399 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002400 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002401 return FPToSI; // FP -> sint
2402 else
2403 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00002404 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002405 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002406 "Casting vector to integer of different width");
Devang Patele9432132008-11-05 01:37:40 +00002407 PTy = NULL;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002408 return BitCast; // Same size, no-op cast
2409 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00002410 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002411 "Casting from a value that is not first-class type");
2412 return PtrToInt; // ptr -> int
2413 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002414 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2415 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002416 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002417 return SIToFP; // sint -> FP
2418 else
2419 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00002420 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002421 if (DestBits < SrcBits) {
2422 return FPTrunc; // FP -> smaller FP
2423 } else if (DestBits > SrcBits) {
2424 return FPExt; // FP -> larger FP
2425 } else {
2426 return BitCast; // same size, no-op cast
2427 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002428 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002429 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002430 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002431 PTy = NULL;
2432 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002433 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002434 llvm_unreachable("Casting pointer or non-first class to float");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002435 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002436 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2437 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002438 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002439 "Casting vector to vector of different widths");
Devang Patelcb181bb2008-11-21 20:00:59 +00002440 SrcPTy = NULL;
Dan Gohmanfead7972007-05-11 21:43:24 +00002441 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002442 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00002443 return BitCast; // float/int -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002444 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00002445 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002446 }
Duncan Sands19d0b472010-02-16 11:11:14 +00002447 } else if (DestTy->isPointerTy()) {
2448 if (SrcTy->isPointerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002449 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00002450 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002451 return IntToPtr; // int -> ptr
2452 } else {
2453 assert(!"Casting pointer to other than pointer or int");
2454 }
2455 } else {
2456 assert(!"Casting to type that is not first-class");
2457 }
2458
2459 // If we fall through to here we probably hit an assertion cast above
2460 // and assertions are not turned on. Anything we return is an error, so
2461 // BitCast is as good a choice as any.
2462 return BitCast;
2463}
2464
2465//===----------------------------------------------------------------------===//
2466// CastInst SubClass Constructors
2467//===----------------------------------------------------------------------===//
2468
2469/// Check that the construction parameters for a CastInst are correct. This
2470/// could be broken out into the separate constructors but it is useful to have
2471/// it in one place and to eliminate the redundant code for getting the sizes
2472/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002473bool
2474CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002475
2476 // Check for type sanity on the arguments
2477 const Type *SrcTy = S->getType();
Chris Lattner37bc78a2010-01-26 21:51:43 +00002478 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
2479 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002480 return false;
2481
2482 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002483 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2484 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002485
2486 // Switch on the opcode provided
2487 switch (op) {
2488 default: return false; // This is an input error
2489 case Instruction::Trunc:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002490 return SrcTy->isIntOrIntVectorTy() &&
2491 DstTy->isIntOrIntVectorTy()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002492 case Instruction::ZExt:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002493 return SrcTy->isIntOrIntVectorTy() &&
2494 DstTy->isIntOrIntVectorTy()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002495 case Instruction::SExt:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002496 return SrcTy->isIntOrIntVectorTy() &&
2497 DstTy->isIntOrIntVectorTy()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002498 case Instruction::FPTrunc:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002499 return SrcTy->isFPOrFPVectorTy() &&
2500 DstTy->isFPOrFPVectorTy() &&
Dan Gohman550c9af2008-08-14 20:04:46 +00002501 SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002502 case Instruction::FPExt:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002503 return SrcTy->isFPOrFPVectorTy() &&
2504 DstTy->isFPOrFPVectorTy() &&
Dan Gohman550c9af2008-08-14 20:04:46 +00002505 SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002506 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002507 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00002508 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2509 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002510 return SVTy->getElementType()->isIntOrIntVectorTy() &&
2511 DVTy->getElementType()->isFPOrFPVectorTy() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002512 SVTy->getNumElements() == DVTy->getNumElements();
2513 }
2514 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002515 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002516 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002517 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00002518 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2519 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002520 return SVTy->getElementType()->isFPOrFPVectorTy() &&
2521 DVTy->getElementType()->isIntOrIntVectorTy() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002522 SVTy->getNumElements() == DVTy->getNumElements();
2523 }
2524 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002525 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002526 case Instruction::PtrToInt:
Duncan Sands19d0b472010-02-16 11:11:14 +00002527 return SrcTy->isPointerTy() && DstTy->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002528 case Instruction::IntToPtr:
Duncan Sands19d0b472010-02-16 11:11:14 +00002529 return SrcTy->isIntegerTy() && DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002530 case Instruction::BitCast:
2531 // BitCast implies a no-op cast of type only. No bits change.
2532 // However, you can't cast pointers to anything but pointers.
Duncan Sands19d0b472010-02-16 11:11:14 +00002533 if (SrcTy->isPointerTy() != DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002534 return false;
2535
Duncan Sands55e50902008-01-06 10:12:28 +00002536 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002537 // these cases, the cast is okay if the source and destination bit widths
2538 // are identical.
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002539 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002540 }
2541}
2542
2543TruncInst::TruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002544 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002545) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002546 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002547}
2548
2549TruncInst::TruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002550 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002551) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002552 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002553}
2554
2555ZExtInst::ZExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002556 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002557) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002558 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002559}
2560
2561ZExtInst::ZExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002562 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002563) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002564 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002565}
2566SExtInst::SExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002567 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002568) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002569 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002570}
2571
Jeff Cohencc08c832006-12-02 02:22:01 +00002572SExtInst::SExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002573 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002574) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002575 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002576}
2577
2578FPTruncInst::FPTruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002579 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002580) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002581 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002582}
2583
2584FPTruncInst::FPTruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002585 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002586) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002587 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002588}
2589
2590FPExtInst::FPExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002591 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002592) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002593 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002594}
2595
2596FPExtInst::FPExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002597 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002598) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002599 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002600}
2601
2602UIToFPInst::UIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002603 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002604) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002605 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002606}
2607
2608UIToFPInst::UIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002609 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002610) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002611 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002612}
2613
2614SIToFPInst::SIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002615 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002616) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002617 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002618}
2619
2620SIToFPInst::SIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002621 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002622) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002623 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002624}
2625
2626FPToUIInst::FPToUIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002627 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002628) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002629 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002630}
2631
2632FPToUIInst::FPToUIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002633 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002634) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002635 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002636}
2637
2638FPToSIInst::FPToSIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002639 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002640) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002641 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002642}
2643
2644FPToSIInst::FPToSIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002645 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002646) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002647 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002648}
2649
2650PtrToIntInst::PtrToIntInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002651 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002652) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002653 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002654}
2655
2656PtrToIntInst::PtrToIntInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002657 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002658) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002659 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002660}
2661
2662IntToPtrInst::IntToPtrInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002663 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002664) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002665 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002666}
2667
2668IntToPtrInst::IntToPtrInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002669 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002670) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002671 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002672}
2673
2674BitCastInst::BitCastInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002675 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002676) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002677 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002678}
2679
2680BitCastInst::BitCastInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002681 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002682) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002683 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002684}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002685
2686//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002687// CmpInst Classes
2688//===----------------------------------------------------------------------===//
2689
Chris Lattneraec33da2010-01-22 06:25:37 +00002690void CmpInst::Anchor() const {}
2691
Nate Begemand2195702008-05-12 19:01:56 +00002692CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002693 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002694 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002695 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002696 OperandTraits<CmpInst>::op_begin(this),
2697 OperandTraits<CmpInst>::operands(this),
2698 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002699 Op<0>() = LHS;
2700 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002701 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002702 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002703}
Gabor Greiff6caff662008-05-10 08:32:32 +00002704
Nate Begemand2195702008-05-12 19:01:56 +00002705CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002706 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002707 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002708 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002709 OperandTraits<CmpInst>::op_begin(this),
2710 OperandTraits<CmpInst>::operands(this),
2711 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002712 Op<0>() = LHS;
2713 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002714 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002715 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002716}
2717
2718CmpInst *
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002719CmpInst::Create(OtherOps Op, unsigned short predicate,
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002720 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002721 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002722 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002723 if (InsertBefore)
2724 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
2725 S1, S2, Name);
2726 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002727 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002728 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002729 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002730
2731 if (InsertBefore)
2732 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
2733 S1, S2, Name);
2734 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002735 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002736 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002737}
2738
2739CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002740CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002741 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002742 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002743 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2744 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002745 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002746 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2747 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002748}
2749
2750void CmpInst::swapOperands() {
2751 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2752 IC->swapOperands();
2753 else
2754 cast<FCmpInst>(this)->swapOperands();
2755}
2756
2757bool CmpInst::isCommutative() {
2758 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2759 return IC->isCommutative();
2760 return cast<FCmpInst>(this)->isCommutative();
2761}
2762
2763bool CmpInst::isEquality() {
2764 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2765 return IC->isEquality();
2766 return cast<FCmpInst>(this)->isEquality();
2767}
2768
2769
Dan Gohman4e724382008-05-31 02:47:54 +00002770CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002771 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002772 default: assert(!"Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002773 case ICMP_EQ: return ICMP_NE;
2774 case ICMP_NE: return ICMP_EQ;
2775 case ICMP_UGT: return ICMP_ULE;
2776 case ICMP_ULT: return ICMP_UGE;
2777 case ICMP_UGE: return ICMP_ULT;
2778 case ICMP_ULE: return ICMP_UGT;
2779 case ICMP_SGT: return ICMP_SLE;
2780 case ICMP_SLT: return ICMP_SGE;
2781 case ICMP_SGE: return ICMP_SLT;
2782 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00002783
Dan Gohman4e724382008-05-31 02:47:54 +00002784 case FCMP_OEQ: return FCMP_UNE;
2785 case FCMP_ONE: return FCMP_UEQ;
2786 case FCMP_OGT: return FCMP_ULE;
2787 case FCMP_OLT: return FCMP_UGE;
2788 case FCMP_OGE: return FCMP_ULT;
2789 case FCMP_OLE: return FCMP_UGT;
2790 case FCMP_UEQ: return FCMP_ONE;
2791 case FCMP_UNE: return FCMP_OEQ;
2792 case FCMP_UGT: return FCMP_OLE;
2793 case FCMP_ULT: return FCMP_OGE;
2794 case FCMP_UGE: return FCMP_OLT;
2795 case FCMP_ULE: return FCMP_OGT;
2796 case FCMP_ORD: return FCMP_UNO;
2797 case FCMP_UNO: return FCMP_ORD;
2798 case FCMP_TRUE: return FCMP_FALSE;
2799 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00002800 }
2801}
2802
Reid Spencer266e42b2006-12-23 06:05:41 +00002803ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2804 switch (pred) {
2805 default: assert(! "Unknown icmp predicate!");
2806 case ICMP_EQ: case ICMP_NE:
2807 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2808 return pred;
2809 case ICMP_UGT: return ICMP_SGT;
2810 case ICMP_ULT: return ICMP_SLT;
2811 case ICMP_UGE: return ICMP_SGE;
2812 case ICMP_ULE: return ICMP_SLE;
2813 }
2814}
2815
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002816ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2817 switch (pred) {
2818 default: assert(! "Unknown icmp predicate!");
2819 case ICMP_EQ: case ICMP_NE:
2820 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2821 return pred;
2822 case ICMP_SGT: return ICMP_UGT;
2823 case ICMP_SLT: return ICMP_ULT;
2824 case ICMP_SGE: return ICMP_UGE;
2825 case ICMP_SLE: return ICMP_ULE;
2826 }
2827}
2828
Reid Spencer0286bc12007-02-28 22:00:54 +00002829/// Initialize a set of values that all satisfy the condition with C.
2830///
2831ConstantRange
2832ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2833 APInt Lower(C);
2834 APInt Upper(C);
2835 uint32_t BitWidth = C.getBitWidth();
2836 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002837 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Reid Spencer0286bc12007-02-28 22:00:54 +00002838 case ICmpInst::ICMP_EQ: Upper++; break;
2839 case ICmpInst::ICMP_NE: Lower++; break;
Dan Gohmand86e2952010-01-26 16:04:20 +00002840 case ICmpInst::ICMP_ULT:
2841 Lower = APInt::getMinValue(BitWidth);
2842 // Check for an empty-set condition.
2843 if (Lower == Upper)
2844 return ConstantRange(BitWidth, /*isFullSet=*/false);
2845 break;
2846 case ICmpInst::ICMP_SLT:
2847 Lower = APInt::getSignedMinValue(BitWidth);
2848 // Check for an empty-set condition.
2849 if (Lower == Upper)
2850 return ConstantRange(BitWidth, /*isFullSet=*/false);
2851 break;
Reid Spencer0286bc12007-02-28 22:00:54 +00002852 case ICmpInst::ICMP_UGT:
2853 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002854 // Check for an empty-set condition.
2855 if (Lower == Upper)
2856 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00002857 break;
2858 case ICmpInst::ICMP_SGT:
2859 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002860 // Check for an empty-set condition.
2861 if (Lower == Upper)
2862 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00002863 break;
2864 case ICmpInst::ICMP_ULE:
2865 Lower = APInt::getMinValue(BitWidth); Upper++;
Dan Gohmand86e2952010-01-26 16:04:20 +00002866 // Check for a full-set condition.
2867 if (Lower == Upper)
2868 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002869 break;
2870 case ICmpInst::ICMP_SLE:
2871 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
Dan Gohmand86e2952010-01-26 16:04:20 +00002872 // Check for a full-set condition.
2873 if (Lower == Upper)
2874 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002875 break;
2876 case ICmpInst::ICMP_UGE:
2877 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002878 // Check for a full-set condition.
2879 if (Lower == Upper)
2880 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002881 break;
2882 case ICmpInst::ICMP_SGE:
2883 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002884 // Check for a full-set condition.
2885 if (Lower == Upper)
2886 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002887 break;
2888 }
2889 return ConstantRange(Lower, Upper);
2890}
2891
Dan Gohman4e724382008-05-31 02:47:54 +00002892CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002893 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002894 default: assert(!"Unknown cmp predicate!");
2895 case ICMP_EQ: case ICMP_NE:
2896 return pred;
2897 case ICMP_SGT: return ICMP_SLT;
2898 case ICMP_SLT: return ICMP_SGT;
2899 case ICMP_SGE: return ICMP_SLE;
2900 case ICMP_SLE: return ICMP_SGE;
2901 case ICMP_UGT: return ICMP_ULT;
2902 case ICMP_ULT: return ICMP_UGT;
2903 case ICMP_UGE: return ICMP_ULE;
2904 case ICMP_ULE: return ICMP_UGE;
2905
Reid Spencerd9436b62006-11-20 01:22:35 +00002906 case FCMP_FALSE: case FCMP_TRUE:
2907 case FCMP_OEQ: case FCMP_ONE:
2908 case FCMP_UEQ: case FCMP_UNE:
2909 case FCMP_ORD: case FCMP_UNO:
2910 return pred;
2911 case FCMP_OGT: return FCMP_OLT;
2912 case FCMP_OLT: return FCMP_OGT;
2913 case FCMP_OGE: return FCMP_OLE;
2914 case FCMP_OLE: return FCMP_OGE;
2915 case FCMP_UGT: return FCMP_ULT;
2916 case FCMP_ULT: return FCMP_UGT;
2917 case FCMP_UGE: return FCMP_ULE;
2918 case FCMP_ULE: return FCMP_UGE;
2919 }
2920}
2921
Reid Spencer266e42b2006-12-23 06:05:41 +00002922bool CmpInst::isUnsigned(unsigned short predicate) {
2923 switch (predicate) {
2924 default: return false;
2925 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2926 case ICmpInst::ICMP_UGE: return true;
2927 }
2928}
2929
Nick Lewycky7494b3b2009-10-25 03:50:03 +00002930bool CmpInst::isSigned(unsigned short predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002931 switch (predicate) {
2932 default: return false;
2933 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2934 case ICmpInst::ICMP_SGE: return true;
2935 }
2936}
2937
2938bool CmpInst::isOrdered(unsigned short predicate) {
2939 switch (predicate) {
2940 default: return false;
2941 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2942 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2943 case FCmpInst::FCMP_ORD: return true;
2944 }
2945}
2946
2947bool CmpInst::isUnordered(unsigned short predicate) {
2948 switch (predicate) {
2949 default: return false;
2950 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2951 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2952 case FCmpInst::FCMP_UNO: return true;
2953 }
2954}
2955
Nick Lewycky7494b3b2009-10-25 03:50:03 +00002956bool CmpInst::isTrueWhenEqual(unsigned short predicate) {
2957 switch(predicate) {
2958 default: return false;
2959 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
2960 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
2961 }
2962}
2963
2964bool CmpInst::isFalseWhenEqual(unsigned short predicate) {
2965 switch(predicate) {
2966 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
2967 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
2968 default: return false;
2969 }
2970}
2971
2972
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002973//===----------------------------------------------------------------------===//
2974// SwitchInst Implementation
2975//===----------------------------------------------------------------------===//
2976
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002977void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002978 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002979 ReservedSpace = 2+NumCases*2;
2980 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00002981 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002982
Gabor Greif2d3024d2008-05-26 21:33:52 +00002983 OperandList[0] = Value;
2984 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002985}
2986
Chris Lattner2195fc42007-02-24 00:55:48 +00002987/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2988/// switch on and a default destination. The number of additional cases can
2989/// be specified here to make memory allocation more efficient. This
2990/// constructor can also autoinsert before another instruction.
2991SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2992 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00002993 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2994 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +00002995 init(Value, Default, NumCases);
2996}
2997
2998/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2999/// switch on and a default destination. The number of additional cases can
3000/// be specified here to make memory allocation more efficient. This
3001/// constructor also autoinserts at the end of the specified BasicBlock.
3002SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3003 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00003004 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
3005 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +00003006 init(Value, Default, NumCases);
3007}
3008
Misha Brukmanb1c93172005-04-21 23:48:37 +00003009SwitchInst::SwitchInst(const SwitchInst &SI)
Owen Anderson55f1c092009-08-13 21:58:54 +00003010 : TerminatorInst(Type::getVoidTy(SI.getContext()), Instruction::Switch,
Gabor Greiff6caff662008-05-10 08:32:32 +00003011 allocHungoffUses(SI.getNumOperands()), SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003012 Use *OL = OperandList, *InOL = SI.OperandList;
3013 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003014 OL[i] = InOL[i];
3015 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003016 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003017 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003018}
3019
Gordon Henriksen14a55692007-12-10 02:14:30 +00003020SwitchInst::~SwitchInst() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003021 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003022}
3023
3024
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003025/// addCase - Add an entry to the switch instruction...
3026///
Chris Lattner47ac1872005-02-24 05:32:09 +00003027void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003028 unsigned OpNo = NumOperands;
3029 if (OpNo+2 > ReservedSpace)
3030 resizeOperands(0); // Get more space!
3031 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003032 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003033 NumOperands = OpNo+2;
Gabor Greif2d3024d2008-05-26 21:33:52 +00003034 OperandList[OpNo] = OnVal;
3035 OperandList[OpNo+1] = Dest;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003036}
3037
3038/// removeCase - This method removes the specified successor from the switch
3039/// instruction. Note that this cannot be used to remove the default
3040/// destination (successor #0).
3041///
3042void SwitchInst::removeCase(unsigned idx) {
3043 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003044 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
3045
3046 unsigned NumOps = getNumOperands();
3047 Use *OL = OperandList;
3048
3049 // Move everything after this operand down.
3050 //
3051 // FIXME: we could just swap with the end of the list, then erase. However,
3052 // client might not expect this to happen. The code as it is thrashes the
3053 // use/def lists, which is kinda lame.
3054 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
3055 OL[i-2] = OL[i];
3056 OL[i-2+1] = OL[i+1];
3057 }
3058
3059 // Nuke the last value.
3060 OL[NumOps-2].set(0);
3061 OL[NumOps-2+1].set(0);
3062 NumOperands = NumOps-2;
3063}
3064
3065/// resizeOperands - resize operands - This adjusts the length of the operands
3066/// list according to the following behavior:
3067/// 1. If NumOps == 0, grow the operand list in response to a push_back style
Gabor Greiff6caff662008-05-10 08:32:32 +00003068/// of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003069/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
3070/// 3. If NumOps == NumOperands, trim the reserved space.
3071///
3072void SwitchInst::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +00003073 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003074 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +00003075 NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003076 } else if (NumOps*2 > NumOperands) {
3077 // No resize needed.
3078 if (ReservedSpace >= NumOps) return;
3079 } else if (NumOps == NumOperands) {
3080 if (ReservedSpace == NumOps) return;
3081 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003082 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003083 }
3084
3085 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00003086 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003087 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00003088 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003089 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003090 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003091 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00003092 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003093}
3094
3095
3096BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3097 return getSuccessor(idx);
3098}
3099unsigned SwitchInst::getNumSuccessorsV() const {
3100 return getNumSuccessors();
3101}
3102void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3103 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003104}
Chris Lattnerf22be932004-10-15 23:52:53 +00003105
Chris Lattner3ed871f2009-10-27 19:13:16 +00003106//===----------------------------------------------------------------------===//
3107// SwitchInst Implementation
3108//===----------------------------------------------------------------------===//
3109
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003110void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003111 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003112 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003113 ReservedSpace = 1+NumDests;
3114 NumOperands = 1;
3115 OperandList = allocHungoffUses(ReservedSpace);
3116
3117 OperandList[0] = Address;
3118}
3119
3120
3121/// resizeOperands - resize operands - This adjusts the length of the operands
3122/// list according to the following behavior:
3123/// 1. If NumOps == 0, grow the operand list in response to a push_back style
3124/// of operation. This grows the number of ops by 2 times.
3125/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
3126/// 3. If NumOps == NumOperands, trim the reserved space.
3127///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003128void IndirectBrInst::resizeOperands(unsigned NumOps) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003129 unsigned e = getNumOperands();
3130 if (NumOps == 0) {
3131 NumOps = e*2;
3132 } else if (NumOps*2 > NumOperands) {
3133 // No resize needed.
3134 if (ReservedSpace >= NumOps) return;
3135 } else if (NumOps == NumOperands) {
3136 if (ReservedSpace == NumOps) return;
3137 } else {
3138 return;
3139 }
3140
3141 ReservedSpace = NumOps;
3142 Use *NewOps = allocHungoffUses(NumOps);
3143 Use *OldOps = OperandList;
3144 for (unsigned i = 0; i != e; ++i)
3145 NewOps[i] = OldOps[i];
3146 OperandList = NewOps;
3147 if (OldOps) Use::zap(OldOps, OldOps + e, true);
3148}
3149
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003150IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3151 Instruction *InsertBefore)
3152: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003153 0, 0, InsertBefore) {
3154 init(Address, NumCases);
3155}
3156
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003157IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3158 BasicBlock *InsertAtEnd)
3159: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003160 0, 0, InsertAtEnd) {
3161 init(Address, NumCases);
3162}
3163
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003164IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
3165 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003166 allocHungoffUses(IBI.getNumOperands()),
3167 IBI.getNumOperands()) {
3168 Use *OL = OperandList, *InOL = IBI.OperandList;
3169 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3170 OL[i] = InOL[i];
3171 SubclassOptionalData = IBI.SubclassOptionalData;
3172}
3173
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003174IndirectBrInst::~IndirectBrInst() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003175 dropHungoffUses(OperandList);
3176}
3177
3178/// addDestination - Add a destination.
3179///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003180void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003181 unsigned OpNo = NumOperands;
3182 if (OpNo+1 > ReservedSpace)
3183 resizeOperands(0); // Get more space!
3184 // Initialize some new operands.
3185 assert(OpNo < ReservedSpace && "Growing didn't work!");
3186 NumOperands = OpNo+1;
3187 OperandList[OpNo] = DestBB;
3188}
3189
3190/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003191/// indirectbr instruction.
3192void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003193 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3194
3195 unsigned NumOps = getNumOperands();
3196 Use *OL = OperandList;
3197
3198 // Replace this value with the last one.
3199 OL[idx+1] = OL[NumOps-1];
3200
3201 // Nuke the last value.
3202 OL[NumOps-1].set(0);
3203 NumOperands = NumOps-1;
3204}
3205
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003206BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003207 return getSuccessor(idx);
3208}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003209unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003210 return getNumSuccessors();
3211}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003212void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003213 setSuccessor(idx, B);
3214}
3215
3216//===----------------------------------------------------------------------===//
Devang Patel11cf3f42009-10-27 22:16:29 +00003217// clone_impl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003218//===----------------------------------------------------------------------===//
3219
Chris Lattnerf22be932004-10-15 23:52:53 +00003220// Define these methods here so vtables don't get emitted into every translation
3221// unit that uses these classes.
3222
Devang Patel11cf3f42009-10-27 22:16:29 +00003223GetElementPtrInst *GetElementPtrInst::clone_impl() const {
3224 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003225}
3226
Devang Patel11cf3f42009-10-27 22:16:29 +00003227BinaryOperator *BinaryOperator::clone_impl() const {
3228 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003229}
3230
Devang Patel11cf3f42009-10-27 22:16:29 +00003231FCmpInst* FCmpInst::clone_impl() const {
3232 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003233}
3234
Devang Patel11cf3f42009-10-27 22:16:29 +00003235ICmpInst* ICmpInst::clone_impl() const {
3236 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003237}
3238
Devang Patel11cf3f42009-10-27 22:16:29 +00003239ExtractValueInst *ExtractValueInst::clone_impl() const {
3240 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003241}
3242
Devang Patel11cf3f42009-10-27 22:16:29 +00003243InsertValueInst *InsertValueInst::clone_impl() const {
3244 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003245}
3246
Devang Patel11cf3f42009-10-27 22:16:29 +00003247AllocaInst *AllocaInst::clone_impl() const {
3248 return new AllocaInst(getAllocatedType(),
3249 (Value*)getOperand(0),
3250 getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003251}
3252
Devang Patel11cf3f42009-10-27 22:16:29 +00003253LoadInst *LoadInst::clone_impl() const {
3254 return new LoadInst(getOperand(0),
3255 Twine(), isVolatile(),
3256 getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003257}
3258
Devang Patel11cf3f42009-10-27 22:16:29 +00003259StoreInst *StoreInst::clone_impl() const {
3260 return new StoreInst(getOperand(0), getOperand(1),
3261 isVolatile(), getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003262}
3263
Devang Patel11cf3f42009-10-27 22:16:29 +00003264TruncInst *TruncInst::clone_impl() const {
3265 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003266}
3267
Devang Patel11cf3f42009-10-27 22:16:29 +00003268ZExtInst *ZExtInst::clone_impl() const {
3269 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003270}
3271
Devang Patel11cf3f42009-10-27 22:16:29 +00003272SExtInst *SExtInst::clone_impl() const {
3273 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003274}
3275
Devang Patel11cf3f42009-10-27 22:16:29 +00003276FPTruncInst *FPTruncInst::clone_impl() const {
3277 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003278}
3279
Devang Patel11cf3f42009-10-27 22:16:29 +00003280FPExtInst *FPExtInst::clone_impl() const {
3281 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003282}
3283
Devang Patel11cf3f42009-10-27 22:16:29 +00003284UIToFPInst *UIToFPInst::clone_impl() const {
3285 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003286}
3287
Devang Patel11cf3f42009-10-27 22:16:29 +00003288SIToFPInst *SIToFPInst::clone_impl() const {
3289 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003290}
3291
Devang Patel11cf3f42009-10-27 22:16:29 +00003292FPToUIInst *FPToUIInst::clone_impl() const {
3293 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003294}
3295
Devang Patel11cf3f42009-10-27 22:16:29 +00003296FPToSIInst *FPToSIInst::clone_impl() const {
3297 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003298}
3299
Devang Patel11cf3f42009-10-27 22:16:29 +00003300PtrToIntInst *PtrToIntInst::clone_impl() const {
3301 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003302}
3303
Devang Patel11cf3f42009-10-27 22:16:29 +00003304IntToPtrInst *IntToPtrInst::clone_impl() const {
3305 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003306}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003307
Devang Patel11cf3f42009-10-27 22:16:29 +00003308BitCastInst *BitCastInst::clone_impl() const {
3309 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003310}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003311
Devang Patel11cf3f42009-10-27 22:16:29 +00003312CallInst *CallInst::clone_impl() const {
3313 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003314}
3315
Devang Patel11cf3f42009-10-27 22:16:29 +00003316SelectInst *SelectInst::clone_impl() const {
3317 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003318}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003319
Devang Patel11cf3f42009-10-27 22:16:29 +00003320VAArgInst *VAArgInst::clone_impl() const {
3321 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003322}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003323
Devang Patel11cf3f42009-10-27 22:16:29 +00003324ExtractElementInst *ExtractElementInst::clone_impl() const {
3325 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003326}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003327
Devang Patel11cf3f42009-10-27 22:16:29 +00003328InsertElementInst *InsertElementInst::clone_impl() const {
3329 return InsertElementInst::Create(getOperand(0),
3330 getOperand(1),
3331 getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003332}
3333
Devang Patel11cf3f42009-10-27 22:16:29 +00003334ShuffleVectorInst *ShuffleVectorInst::clone_impl() const {
3335 return new ShuffleVectorInst(getOperand(0),
3336 getOperand(1),
3337 getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003338}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003339
Devang Patel11cf3f42009-10-27 22:16:29 +00003340PHINode *PHINode::clone_impl() const {
3341 return new PHINode(*this);
3342}
3343
3344ReturnInst *ReturnInst::clone_impl() const {
3345 return new(getNumOperands()) ReturnInst(*this);
3346}
3347
3348BranchInst *BranchInst::clone_impl() const {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00003349 unsigned Ops(getNumOperands());
Devang Patel11cf3f42009-10-27 22:16:29 +00003350 return new(Ops, Ops == 1) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003351}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003352
Devang Patel11cf3f42009-10-27 22:16:29 +00003353SwitchInst *SwitchInst::clone_impl() const {
3354 return new SwitchInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003355}
3356
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003357IndirectBrInst *IndirectBrInst::clone_impl() const {
3358 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003359}
3360
3361
Devang Patel11cf3f42009-10-27 22:16:29 +00003362InvokeInst *InvokeInst::clone_impl() const {
3363 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003364}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003365
Devang Patel11cf3f42009-10-27 22:16:29 +00003366UnwindInst *UnwindInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003367 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003368 return new UnwindInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003369}
3370
Devang Patel11cf3f42009-10-27 22:16:29 +00003371UnreachableInst *UnreachableInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003372 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003373 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003374}