blob: 6aef9c748401a779a738b8fcd392c274d73b6fa0 [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 Greif33ae80b2010-04-15 20:51:13 +000036 ? cast<CallInst>(II)->op_end() - 1 // Skip Function
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000037 : cast<InvokeInst>(II)->op_end() - 3; // Skip BB, BB, Function
38}
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 Greif33ae80b2010-04-15 20:51:13 +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!");
Gabor Greif33ae80b2010-04-15 20:51:13 +0000243
244 Use *OL = OperandList;
Chris Lattner054ba2c2007-02-13 00:58:44 +0000245 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000246 assert((i >= FTy->getNumParams() ||
247 FTy->getParamType(i) == Params[i]->getType()) &&
248 "Calling a function with a bad signature!");
Gabor Greif33ae80b2010-04-15 20:51:13 +0000249 OL[i] = Params[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000250 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000251}
252
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000253void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000254 assert(NumOperands == 3 && "NumOperands not set up?");
Gabor Greif33ae80b2010-04-15 20:51:13 +0000255 Op<-1>() = Func;
256 Op<0>() = Actual1;
257 Op<1>() = Actual2;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000258
259 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000260 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000261 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000262
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000263 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000264 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000265 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000266 assert((0 >= FTy->getNumParams() ||
267 FTy->getParamType(0) == Actual1->getType()) &&
268 "Calling a function with a bad signature!");
269 assert((1 >= FTy->getNumParams() ||
270 FTy->getParamType(1) == Actual2->getType()) &&
271 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000272}
273
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000274void CallInst::init(Value *Func, Value *Actual) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000275 assert(NumOperands == 2 && "NumOperands not set up?");
Gabor Greif33ae80b2010-04-15 20:51:13 +0000276 Op<-1>() = Func;
277 Op<0>() = Actual;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000278
279 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000280 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000281 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000282
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000283 assert((FTy->getNumParams() == 1 ||
284 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000285 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000286 assert((0 == FTy->getNumParams() ||
287 FTy->getParamType(0) == Actual->getType()) &&
288 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000289}
290
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000291void CallInst::init(Value *Func) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000292 assert(NumOperands == 1 && "NumOperands not set up?");
Gabor Greif33ae80b2010-04-15 20:51:13 +0000293 Op<-1>() = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000294
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000295 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000296 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000297 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000298
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000299 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000300}
301
Daniel Dunbar4975db62009-07-25 04:41:11 +0000302CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +0000303 Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000304 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
305 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000306 Instruction::Call,
307 OperandTraits<CallInst>::op_end(this) - 2,
308 2, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000309 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000310 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000311}
312
Daniel Dunbar4975db62009-07-25 04:41:11 +0000313CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000314 BasicBlock *InsertAtEnd)
315 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
316 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000317 Instruction::Call,
318 OperandTraits<CallInst>::op_end(this) - 2,
319 2, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000320 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000321 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000322}
Daniel Dunbar4975db62009-07-25 04:41:11 +0000323CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000324 Instruction *InsertBefore)
325 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
326 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000327 Instruction::Call,
328 OperandTraits<CallInst>::op_end(this) - 1,
329 1, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000330 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000331 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000332}
333
Daniel Dunbar4975db62009-07-25 04:41:11 +0000334CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000335 BasicBlock *InsertAtEnd)
336 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
337 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000338 Instruction::Call,
339 OperandTraits<CallInst>::op_end(this) - 1,
340 1, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000341 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000342 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000343}
344
Misha Brukmanb1c93172005-04-21 23:48:37 +0000345CallInst::CallInst(const CallInst &CI)
Gabor Greiff6caff662008-05-10 08:32:32 +0000346 : Instruction(CI.getType(), Instruction::Call,
347 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000348 CI.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000349 setAttributes(CI.getAttributes());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000350 setTailCall(CI.isTailCall());
351 setCallingConv(CI.getCallingConv());
352
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000353 Use *OL = OperandList;
354 Use *InOL = CI.OperandList;
355 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000356 OL[i] = InOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000357 SubclassOptionalData = CI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000358}
359
Devang Patel4c758ea2008-09-25 21:00:45 +0000360void CallInst::addAttribute(unsigned i, Attributes attr) {
361 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000362 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000363 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000364}
365
Devang Patel4c758ea2008-09-25 21:00:45 +0000366void CallInst::removeAttribute(unsigned i, Attributes attr) {
367 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000368 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000369 setAttributes(PAL);
Duncan Sands78c88722008-07-08 08:38:44 +0000370}
371
Devang Patelba3fa6c2008-09-23 23:03:40 +0000372bool CallInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000373 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000374 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000375 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000376 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000377 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000378}
379
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000380/// IsConstantOne - Return true only if val is constant int 1
381static bool IsConstantOne(Value *val) {
382 assert(val && "IsConstantOne does not work with NULL val");
383 return isa<ConstantInt>(val) && cast<ConstantInt>(val)->isOne();
384}
385
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000386static Instruction *createMalloc(Instruction *InsertBefore,
387 BasicBlock *InsertAtEnd, const Type *IntPtrTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000388 const Type *AllocTy, Value *AllocSize,
389 Value *ArraySize, Function *MallocF,
390 const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000391 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000392 "createMalloc needs either InsertBefore or InsertAtEnd");
393
394 // malloc(type) becomes:
395 // bitcast (i8* malloc(typeSize)) to type*
396 // malloc(type, arraySize) becomes:
397 // bitcast (i8 *malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000398 if (!ArraySize)
399 ArraySize = ConstantInt::get(IntPtrTy, 1);
400 else if (ArraySize->getType() != IntPtrTy) {
401 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000402 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
403 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000404 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000405 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
406 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000407 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000408
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000409 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000410 if (IsConstantOne(AllocSize)) {
411 AllocSize = ArraySize; // Operand * 1 = Operand
412 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
413 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
414 false /*ZExt*/);
415 // Malloc arg is constant product of type size and array size
416 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
417 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000418 // Multiply type size by the array size...
419 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000420 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
421 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000422 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000423 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
424 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000425 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000426 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000427
Victor Hernandez788eaab2009-09-18 19:20:02 +0000428 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000429 // Create the call to Malloc.
430 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
431 Module* M = BB->getParent()->getParent();
Duncan Sands9ed7b162009-10-06 15:40:36 +0000432 const Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezbb336a12009-11-10 19:53:28 +0000433 Value *MallocFunc = MallocF;
434 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000435 // prototype malloc as "void *malloc(size_t)"
Victor Hernandezbb336a12009-11-10 19:53:28 +0000436 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, NULL);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000437 const PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000438 CallInst *MCall = NULL;
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000439 Instruction *Result = NULL;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000440 if (InsertBefore) {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000441 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall", InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000442 Result = MCall;
443 if (Result->getType() != AllocPtrType)
444 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000445 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000446 } else {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000447 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000448 Result = MCall;
449 if (Result->getType() != AllocPtrType) {
450 InsertAtEnd->getInstList().push_back(MCall);
451 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000452 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000453 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000454 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000455 MCall->setTailCall();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000456 if (Function *F = dyn_cast<Function>(MallocFunc)) {
457 MCall->setCallingConv(F->getCallingConv());
458 if (!F->doesNotAlias(0)) F->setDoesNotAlias(0);
459 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000460 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000461
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000462 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000463}
464
465/// CreateMalloc - Generate the IR for a call to malloc:
466/// 1. Compute the malloc call's argument as the specified type's size,
467/// possibly multiplied by the array size if the array size is not
468/// constant 1.
469/// 2. Call malloc with that argument.
470/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000471Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
472 const Type *IntPtrTy, const Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000473 Value *AllocSize, Value *ArraySize,
474 const Twine &Name) {
475 return createMalloc(InsertBefore, NULL, IntPtrTy, AllocTy, AllocSize,
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000476 ArraySize, NULL, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000477}
478
479/// CreateMalloc - Generate the IR for a call to malloc:
480/// 1. Compute the malloc call's argument as the specified type's size,
481/// possibly multiplied by the array size if the array size is not
482/// constant 1.
483/// 2. Call malloc with that argument.
484/// 3. Bitcast the result of the malloc call to the specified type.
485/// Note: This function does not add the bitcast to the basic block, that is the
486/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000487Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
488 const Type *IntPtrTy, const Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000489 Value *AllocSize, Value *ArraySize,
490 Function *MallocF, const Twine &Name) {
491 return createMalloc(NULL, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000492 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000493}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000494
Victor Hernandeze2971492009-10-24 04:23:03 +0000495static Instruction* createFree(Value* Source, Instruction *InsertBefore,
496 BasicBlock *InsertAtEnd) {
497 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
498 "createFree needs either InsertBefore or InsertAtEnd");
Duncan Sands19d0b472010-02-16 11:11:14 +0000499 assert(Source->getType()->isPointerTy() &&
Victor Hernandeze2971492009-10-24 04:23:03 +0000500 "Can not free something of nonpointer type!");
501
502 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
503 Module* M = BB->getParent()->getParent();
504
505 const Type *VoidTy = Type::getVoidTy(M->getContext());
506 const Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
507 // prototype free as "void free(void*)"
Chris Lattner2156c222009-11-09 07:12:01 +0000508 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, NULL);
Victor Hernandeze2971492009-10-24 04:23:03 +0000509 CallInst* Result = NULL;
510 Value *PtrCast = Source;
511 if (InsertBefore) {
512 if (Source->getType() != IntPtrTy)
513 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
514 Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore);
515 } else {
516 if (Source->getType() != IntPtrTy)
517 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
518 Result = CallInst::Create(FreeFunc, PtrCast, "");
519 }
520 Result->setTailCall();
Chris Lattner2156c222009-11-09 07:12:01 +0000521 if (Function *F = dyn_cast<Function>(FreeFunc))
522 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000523
524 return Result;
525}
526
527/// CreateFree - Generate the IR for a call to the builtin free function.
528void CallInst::CreateFree(Value* Source, Instruction *InsertBefore) {
529 createFree(Source, InsertBefore, NULL);
530}
531
532/// CreateFree - Generate the IR for a call to the builtin free function.
533/// Note: This function does not add the call to the basic block, that is the
534/// responsibility of the caller.
535Instruction* CallInst::CreateFree(Value* Source, BasicBlock *InsertAtEnd) {
536 Instruction* FreeCall = createFree(Source, NULL, InsertAtEnd);
537 assert(FreeCall && "CreateFree did not create a CallInst");
538 return FreeCall;
539}
540
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000541//===----------------------------------------------------------------------===//
542// InvokeInst Implementation
543//===----------------------------------------------------------------------===//
544
545void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000546 Value* const *Args, unsigned NumArgs) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000547 assert(NumOperands == 3+NumArgs && "NumOperands not set up?");
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000548 Op<-3>() = Fn;
549 Op<-2>() = IfNormal;
550 Op<-1>() = IfException;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000551 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000552 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000553 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000554
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000555 assert(((NumArgs == FTy->getNumParams()) ||
556 (FTy->isVarArg() && NumArgs > FTy->getNumParams())) &&
Gabor Greif668d7002010-03-23 13:45:54 +0000557 "Invoking a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000558
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000559 Use *OL = OperandList;
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000560 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000561 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000562 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000563 "Invoking a function with a bad signature!");
564
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000565 OL[i] = Args[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000566 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000567}
568
Misha Brukmanb1c93172005-04-21 23:48:37 +0000569InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000570 : TerminatorInst(II.getType(), Instruction::Invoke,
Gabor Greif697e94c2008-05-15 10:04:30 +0000571 OperandTraits<InvokeInst>::op_end(this)
572 - II.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000573 II.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000574 setAttributes(II.getAttributes());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000575 setCallingConv(II.getCallingConv());
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000576 Use *OL = OperandList, *InOL = II.OperandList;
577 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000578 OL[i] = InOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000579 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000580}
581
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000582BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
583 return getSuccessor(idx);
584}
585unsigned InvokeInst::getNumSuccessorsV() const {
586 return getNumSuccessors();
587}
588void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
589 return setSuccessor(idx, B);
590}
591
Devang Patelba3fa6c2008-09-23 23:03:40 +0000592bool InvokeInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000593 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000594 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000595 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000596 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000597 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000598}
599
Devang Patel4c758ea2008-09-25 21:00:45 +0000600void InvokeInst::addAttribute(unsigned i, Attributes attr) {
601 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000602 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000603 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000604}
605
Devang Patel4c758ea2008-09-25 21:00:45 +0000606void InvokeInst::removeAttribute(unsigned i, Attributes attr) {
607 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000608 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000609 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000610}
611
Duncan Sands5208d1a2007-11-28 17:07:01 +0000612
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000613//===----------------------------------------------------------------------===//
614// ReturnInst Implementation
615//===----------------------------------------------------------------------===//
616
Chris Lattner2195fc42007-02-24 00:55:48 +0000617ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000618 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000619 OperandTraits<ReturnInst>::op_end(this) -
620 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000621 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000622 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000623 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000624 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000625}
626
Owen Anderson55f1c092009-08-13 21:58:54 +0000627ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
628 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000629 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
630 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000631 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000632 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000633}
Owen Anderson55f1c092009-08-13 21:58:54 +0000634ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
635 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000636 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
637 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000638 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000639 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000640}
Owen Anderson55f1c092009-08-13 21:58:54 +0000641ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
642 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000643 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000644}
645
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000646unsigned ReturnInst::getNumSuccessorsV() const {
647 return getNumSuccessors();
648}
649
Devang Patelae682fb2008-02-26 17:56:20 +0000650/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
651/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000652void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000653 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000654}
655
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000656BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000657 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000658 return 0;
659}
660
Devang Patel59643e52008-02-23 00:35:18 +0000661ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000662}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000663
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000664//===----------------------------------------------------------------------===//
665// UnwindInst Implementation
666//===----------------------------------------------------------------------===//
667
Owen Anderson55f1c092009-08-13 21:58:54 +0000668UnwindInst::UnwindInst(LLVMContext &Context, Instruction *InsertBefore)
669 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
670 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000671}
Owen Anderson55f1c092009-08-13 21:58:54 +0000672UnwindInst::UnwindInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
673 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
674 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000675}
676
677
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000678unsigned UnwindInst::getNumSuccessorsV() const {
679 return getNumSuccessors();
680}
681
682void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000683 llvm_unreachable("UnwindInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000684}
685
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000686BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000687 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000688 return 0;
689}
690
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000691//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000692// UnreachableInst Implementation
693//===----------------------------------------------------------------------===//
694
Owen Anderson55f1c092009-08-13 21:58:54 +0000695UnreachableInst::UnreachableInst(LLVMContext &Context,
696 Instruction *InsertBefore)
697 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
698 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000699}
Owen Anderson55f1c092009-08-13 21:58:54 +0000700UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
701 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
702 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000703}
704
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000705unsigned UnreachableInst::getNumSuccessorsV() const {
706 return getNumSuccessors();
707}
708
709void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000710 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000711}
712
713BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000714 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000715 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000716}
717
718//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000719// BranchInst Implementation
720//===----------------------------------------------------------------------===//
721
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000722void BranchInst::AssertOK() {
723 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +0000724 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000725 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000726}
727
Chris Lattner2195fc42007-02-24 00:55:48 +0000728BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000729 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000730 OperandTraits<BranchInst>::op_end(this) - 1,
731 1, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000732 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000733 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000734}
735BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
736 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000737 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000738 OperandTraits<BranchInst>::op_end(this) - 3,
739 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000740 Op<-1>() = IfTrue;
741 Op<-2>() = IfFalse;
742 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000743#ifndef NDEBUG
744 AssertOK();
745#endif
746}
747
748BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000749 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000750 OperandTraits<BranchInst>::op_end(this) - 1,
751 1, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000752 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000753 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000754}
755
756BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
757 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000758 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000759 OperandTraits<BranchInst>::op_end(this) - 3,
760 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000761 Op<-1>() = IfTrue;
762 Op<-2>() = IfFalse;
763 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000764#ifndef NDEBUG
765 AssertOK();
766#endif
767}
768
769
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000770BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +0000771 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000772 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
773 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000774 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000775 if (BI.getNumOperands() != 1) {
776 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000777 Op<-3>() = BI.Op<-3>();
778 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000779 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000780 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000781}
782
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000783
784Use* Use::getPrefix() {
785 PointerIntPair<Use**, 2, PrevPtrTag> &PotentialPrefix(this[-1].Prev);
786 if (PotentialPrefix.getOpaqueValue())
787 return 0;
788
789 return reinterpret_cast<Use*>((char*)&PotentialPrefix + 1);
790}
791
792BranchInst::~BranchInst() {
793 if (NumOperands == 1) {
794 if (Use *Prefix = OperandList->getPrefix()) {
795 Op<-1>() = 0;
796 //
797 // mark OperandList to have a special value for scrutiny
798 // by baseclass destructors and operator delete
799 OperandList = Prefix;
800 } else {
801 NumOperands = 3;
802 OperandList = op_begin();
803 }
804 }
805}
806
807
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000808BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
809 return getSuccessor(idx);
810}
811unsigned BranchInst::getNumSuccessorsV() const {
812 return getNumSuccessors();
813}
814void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
815 setSuccessor(idx, B);
816}
817
818
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000819//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +0000820// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000821//===----------------------------------------------------------------------===//
822
Owen Andersonb6b25302009-07-14 23:09:55 +0000823static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000824 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +0000825 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000826 else {
827 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000828 "Passed basic block into allocation size parameter! Use other ctor");
Duncan Sands9dff9be2010-02-15 16:12:20 +0000829 assert(Amt->getType()->isIntegerTy(32) &&
Victor Hernandeza3aaf852009-10-17 01:18:07 +0000830 "Allocation array size is not a 32-bit integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000831 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000832 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000833}
834
Victor Hernandez8acf2952009-10-23 21:09:37 +0000835AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize,
836 const Twine &Name, Instruction *InsertBefore)
837 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
838 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
839 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000840 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000841 setName(Name);
842}
843
844AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize,
845 const Twine &Name, BasicBlock *InsertAtEnd)
846 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
847 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
848 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000849 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000850 setName(Name);
851}
852
853AllocaInst::AllocaInst(const Type *Ty, const Twine &Name,
854 Instruction *InsertBefore)
855 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
856 getAISize(Ty->getContext(), 0), InsertBefore) {
857 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000858 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000859 setName(Name);
860}
861
862AllocaInst::AllocaInst(const Type *Ty, const Twine &Name,
863 BasicBlock *InsertAtEnd)
864 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
865 getAISize(Ty->getContext(), 0), InsertAtEnd) {
866 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000867 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000868 setName(Name);
869}
870
871AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
872 const Twine &Name, Instruction *InsertBefore)
873 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000874 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000875 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000876 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000877 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000878}
879
Victor Hernandez8acf2952009-10-23 21:09:37 +0000880AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
881 const Twine &Name, BasicBlock *InsertAtEnd)
882 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000883 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000884 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000885 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000886 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000887}
888
Gordon Henriksen14a55692007-12-10 02:14:30 +0000889// Out of line virtual method, so the vtable, etc has a home.
Victor Hernandez8acf2952009-10-23 21:09:37 +0000890AllocaInst::~AllocaInst() {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000891}
892
Victor Hernandez8acf2952009-10-23 21:09:37 +0000893void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000894 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +0000895 setInstructionSubclassData(Log2_32(Align) + 1);
Dan Gohmanaa583d72008-03-24 16:55:58 +0000896 assert(getAlignment() == Align && "Alignment representation error!");
897}
898
Victor Hernandez8acf2952009-10-23 21:09:37 +0000899bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000900 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
901 return CI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000902 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000903}
904
Victor Hernandez8acf2952009-10-23 21:09:37 +0000905const Type *AllocaInst::getAllocatedType() const {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000906 return getType()->getElementType();
907}
908
Chris Lattner8b291e62008-11-26 02:54:17 +0000909/// isStaticAlloca - Return true if this alloca is in the entry block of the
910/// function and is a constant size. If so, the code generator will fold it
911/// into the prolog/epilog code, so it is basically free.
912bool AllocaInst::isStaticAlloca() const {
913 // Must be constant size.
914 if (!isa<ConstantInt>(getArraySize())) return false;
915
916 // Must be in the entry block.
917 const BasicBlock *Parent = getParent();
918 return Parent == &Parent->getParent()->front();
919}
920
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000921//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000922// LoadInst Implementation
923//===----------------------------------------------------------------------===//
924
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000925void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +0000926 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000927 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000928}
929
Daniel Dunbar4975db62009-07-25 04:41:11 +0000930LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000931 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000932 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000933 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000934 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000935 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000936 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000937}
938
Daniel Dunbar4975db62009-07-25 04:41:11 +0000939LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000940 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000941 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000942 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000943 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000944 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000945 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000946}
947
Daniel Dunbar4975db62009-07-25 04:41:11 +0000948LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000949 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000950 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000951 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000952 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000953 setAlignment(0);
954 AssertOK();
955 setName(Name);
956}
957
Daniel Dunbar4975db62009-07-25 04:41:11 +0000958LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +0000959 unsigned Align, Instruction *InsertBef)
960 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
961 Load, Ptr, InsertBef) {
962 setVolatile(isVolatile);
963 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000964 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000965 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000966}
967
Daniel Dunbar4975db62009-07-25 04:41:11 +0000968LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000969 unsigned Align, BasicBlock *InsertAE)
970 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
971 Load, Ptr, InsertAE) {
972 setVolatile(isVolatile);
973 setAlignment(Align);
974 AssertOK();
975 setName(Name);
976}
977
Daniel Dunbar4975db62009-07-25 04:41:11 +0000978LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000979 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000980 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000981 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000982 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000983 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000984 AssertOK();
985 setName(Name);
986}
987
Daniel Dunbar27096822009-08-11 18:11:15 +0000988
989
990LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
991 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
992 Load, Ptr, InsertBef) {
993 setVolatile(false);
994 setAlignment(0);
995 AssertOK();
996 if (Name && Name[0]) setName(Name);
997}
998
999LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
1000 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1001 Load, Ptr, InsertAE) {
1002 setVolatile(false);
1003 setAlignment(0);
1004 AssertOK();
1005 if (Name && Name[0]) setName(Name);
1006}
1007
1008LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1009 Instruction *InsertBef)
1010: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1011 Load, Ptr, InsertBef) {
1012 setVolatile(isVolatile);
1013 setAlignment(0);
1014 AssertOK();
1015 if (Name && Name[0]) setName(Name);
1016}
1017
1018LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1019 BasicBlock *InsertAE)
1020 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1021 Load, Ptr, InsertAE) {
1022 setVolatile(isVolatile);
1023 setAlignment(0);
1024 AssertOK();
1025 if (Name && Name[0]) setName(Name);
1026}
1027
Christopher Lamb84485702007-04-22 19:24:39 +00001028void LoadInst::setAlignment(unsigned Align) {
1029 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001030 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
1031 ((Log2_32(Align)+1)<<1));
Christopher Lamb84485702007-04-22 19:24:39 +00001032}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001033
1034//===----------------------------------------------------------------------===//
1035// StoreInst Implementation
1036//===----------------------------------------------------------------------===//
1037
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001038void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001039 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001040 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001041 "Ptr must have pointer type!");
1042 assert(getOperand(0)->getType() ==
1043 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001044 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001045}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001046
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001047
1048StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001049 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001050 OperandTraits<StoreInst>::op_begin(this),
1051 OperandTraits<StoreInst>::operands(this),
1052 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001053 Op<0>() = val;
1054 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001055 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001056 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001057 AssertOK();
1058}
1059
1060StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001061 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001062 OperandTraits<StoreInst>::op_begin(this),
1063 OperandTraits<StoreInst>::operands(this),
1064 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001065 Op<0>() = val;
1066 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001067 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001068 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001069 AssertOK();
1070}
1071
Misha Brukmanb1c93172005-04-21 23:48:37 +00001072StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001073 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001074 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001075 OperandTraits<StoreInst>::op_begin(this),
1076 OperandTraits<StoreInst>::operands(this),
1077 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001078 Op<0>() = val;
1079 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001080 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001081 setAlignment(0);
1082 AssertOK();
1083}
1084
1085StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1086 unsigned Align, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001087 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001088 OperandTraits<StoreInst>::op_begin(this),
1089 OperandTraits<StoreInst>::operands(this),
1090 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001091 Op<0>() = val;
1092 Op<1>() = addr;
Christopher Lamb84485702007-04-22 19:24:39 +00001093 setVolatile(isVolatile);
1094 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001095 AssertOK();
1096}
1097
Misha Brukmanb1c93172005-04-21 23:48:37 +00001098StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001099 unsigned Align, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001100 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001101 OperandTraits<StoreInst>::op_begin(this),
1102 OperandTraits<StoreInst>::operands(this),
1103 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001104 Op<0>() = val;
1105 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001106 setVolatile(isVolatile);
1107 setAlignment(Align);
1108 AssertOK();
1109}
1110
1111StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001112 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001113 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001114 OperandTraits<StoreInst>::op_begin(this),
1115 OperandTraits<StoreInst>::operands(this),
1116 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001117 Op<0>() = val;
1118 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001119 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001120 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001121 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001122}
1123
Christopher Lamb84485702007-04-22 19:24:39 +00001124void StoreInst::setAlignment(unsigned Align) {
1125 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001126 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
1127 ((Log2_32(Align)+1) << 1));
Christopher Lamb84485702007-04-22 19:24:39 +00001128}
1129
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001130//===----------------------------------------------------------------------===//
1131// GetElementPtrInst Implementation
1132//===----------------------------------------------------------------------===//
1133
Christopher Lambedf07882007-12-17 01:12:55 +00001134static unsigned retrieveAddrSpace(const Value *Val) {
1135 return cast<PointerType>(Val->getType())->getAddressSpace();
1136}
1137
Gabor Greif21ba1842008-06-06 20:28:12 +00001138void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001139 const Twine &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001140 assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
1141 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001142 OL[0] = Ptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001143
Chris Lattner79807c3d2007-01-31 19:47:18 +00001144 for (unsigned i = 0; i != NumIdx; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001145 OL[i+1] = Idx[i];
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001146
1147 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001148}
1149
Daniel Dunbar4975db62009-07-25 04:41:11 +00001150void GetElementPtrInst::init(Value *Ptr, Value *Idx, const Twine &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001151 assert(NumOperands == 2 && "NumOperands not initialized?");
1152 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001153 OL[0] = Ptr;
1154 OL[1] = Idx;
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001155
1156 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001157}
1158
Gabor Greiff6caff662008-05-10 08:32:32 +00001159GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
Gabor Greife9408e62008-05-27 11:03:29 +00001160 : Instruction(GEPI.getType(), GetElementPtr,
Gabor Greif697e94c2008-05-15 10:04:30 +00001161 OperandTraits<GetElementPtrInst>::op_end(this)
1162 - GEPI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001163 GEPI.getNumOperands()) {
1164 Use *OL = OperandList;
1165 Use *GEPIOL = GEPI.OperandList;
1166 for (unsigned i = 0, E = NumOperands; i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001167 OL[i] = GEPIOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001168 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001169}
1170
Chris Lattner82981202005-05-03 05:43:30 +00001171GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001172 const Twine &Name, Instruction *InBe)
Owen Anderson4056ca92009-07-29 22:17:13 +00001173 : Instruction(PointerType::get(
Owen Andersond420fd42009-07-16 00:03:07 +00001174 checkType(getIndexedType(Ptr->getType(),Idx)), retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001175 GetElementPtr,
1176 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1177 2, InBe) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001178 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001179}
1180
1181GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001182 const Twine &Name, BasicBlock *IAE)
Owen Anderson4056ca92009-07-29 22:17:13 +00001183 : Instruction(PointerType::get(
Owen Andersond420fd42009-07-16 00:03:07 +00001184 checkType(getIndexedType(Ptr->getType(),Idx)),
1185 retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001186 GetElementPtr,
1187 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1188 2, IAE) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001189 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001190}
1191
Chris Lattner6090a422009-03-09 04:46:40 +00001192/// getIndexedType - Returns the type of the element that would be accessed with
1193/// a gep instruction with the specified parameters.
1194///
1195/// The Idxs pointer should point to a continuous piece of memory containing the
1196/// indices, either as Value* or uint64_t.
1197///
1198/// A null type is returned if the indices are invalid for the specified
1199/// pointer type.
1200///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001201template <typename IndexTy>
Chris Lattner6090a422009-03-09 04:46:40 +00001202static const Type* getIndexedTypeInternal(const Type *Ptr, IndexTy const *Idxs,
1203 unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001204 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1205 if (!PTy) return 0; // Type isn't a pointer type!
1206 const Type *Agg = PTy->getElementType();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001207
Chris Lattner6090a422009-03-09 04:46:40 +00001208 // Handle the special case of the empty set index set, which is always valid.
Dan Gohman12fce772008-05-15 19:50:34 +00001209 if (NumIdx == 0)
1210 return Agg;
Chris Lattner6090a422009-03-09 04:46:40 +00001211
1212 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattner4a488152009-03-09 04:56:22 +00001213 // it cannot be 'stepped over'. We explicitly allow abstract types (those
1214 // that contain opaque types) under the assumption that it will be resolved to
1215 // a sane type later.
1216 if (!Agg->isSized() && !Agg->isAbstract())
Chris Lattner6090a422009-03-09 04:46:40 +00001217 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001218
Dan Gohman1ecaf452008-05-31 00:58:22 +00001219 unsigned CurIdx = 1;
1220 for (; CurIdx != NumIdx; ++CurIdx) {
1221 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
Duncan Sands19d0b472010-02-16 11:11:14 +00001222 if (!CT || CT->isPointerTy()) return 0;
Matthijs Kooijman04468622008-07-29 08:46:11 +00001223 IndexTy Index = Idxs[CurIdx];
Dan Gohman1ecaf452008-05-31 00:58:22 +00001224 if (!CT->indexValid(Index)) return 0;
1225 Agg = CT->getTypeAtIndex(Index);
1226
1227 // If the new type forwards to another type, then it is in the middle
1228 // of being refined to another type (and hence, may have dropped all
1229 // references to what it was using before). So, use the new forwarded
1230 // type.
1231 if (const Type *Ty = Agg->getForwardedType())
1232 Agg = Ty;
1233 }
1234 return CurIdx == NumIdx ? Agg : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001235}
1236
Matthijs Kooijman04468622008-07-29 08:46:11 +00001237const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1238 Value* const *Idxs,
1239 unsigned NumIdx) {
1240 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1241}
1242
1243const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1244 uint64_t const *Idxs,
1245 unsigned NumIdx) {
1246 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1247}
1248
Chris Lattner82981202005-05-03 05:43:30 +00001249const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1250 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1251 if (!PTy) return 0; // Type isn't a pointer type!
1252
1253 // Check the pointer index.
1254 if (!PTy->indexValid(Idx)) return 0;
1255
Chris Lattnerc2233332005-05-03 16:44:45 +00001256 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +00001257}
1258
Chris Lattner45f15572007-04-14 00:12:57 +00001259
1260/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1261/// zeros. If so, the result pointer and the first operand have the same
1262/// value, just potentially different types.
1263bool GetElementPtrInst::hasAllZeroIndices() const {
1264 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1265 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1266 if (!CI->isZero()) return false;
1267 } else {
1268 return false;
1269 }
1270 }
1271 return true;
1272}
1273
Chris Lattner27058292007-04-27 20:35:56 +00001274/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1275/// constant integers. If so, the result pointer and the first operand have
1276/// a constant offset between them.
1277bool GetElementPtrInst::hasAllConstantIndices() const {
1278 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1279 if (!isa<ConstantInt>(getOperand(i)))
1280 return false;
1281 }
1282 return true;
1283}
1284
Dan Gohman1b849082009-09-07 23:54:19 +00001285void GetElementPtrInst::setIsInBounds(bool B) {
1286 cast<GEPOperator>(this)->setIsInBounds(B);
1287}
Chris Lattner45f15572007-04-14 00:12:57 +00001288
Nick Lewycky28a5f252009-09-27 21:33:04 +00001289bool GetElementPtrInst::isInBounds() const {
1290 return cast<GEPOperator>(this)->isInBounds();
1291}
1292
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001293//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001294// ExtractElementInst Implementation
1295//===----------------------------------------------------------------------===//
1296
1297ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001298 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001299 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001300 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001301 ExtractElement,
1302 OperandTraits<ExtractElementInst>::op_begin(this),
1303 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001304 assert(isValidOperands(Val, Index) &&
1305 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001306 Op<0>() = Val;
1307 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001308 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001309}
1310
1311ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001312 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001313 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001314 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001315 ExtractElement,
1316 OperandTraits<ExtractElementInst>::op_begin(this),
1317 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001318 assert(isValidOperands(Val, Index) &&
1319 "Invalid extractelement instruction operands!");
1320
Gabor Greif2d3024d2008-05-26 21:33:52 +00001321 Op<0>() = Val;
1322 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001323 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001324}
1325
Chris Lattner65511ff2006-10-05 06:24:58 +00001326
Chris Lattner54865b32006-04-08 04:05:48 +00001327bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001328 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy(32))
Chris Lattner54865b32006-04-08 04:05:48 +00001329 return false;
1330 return true;
1331}
1332
1333
Robert Bocchino23004482006-01-10 19:05:34 +00001334//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001335// InsertElementInst Implementation
1336//===----------------------------------------------------------------------===//
1337
Chris Lattner54865b32006-04-08 04:05:48 +00001338InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001339 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001340 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001341 : Instruction(Vec->getType(), InsertElement,
1342 OperandTraits<InsertElementInst>::op_begin(this),
1343 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001344 assert(isValidOperands(Vec, Elt, Index) &&
1345 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001346 Op<0>() = Vec;
1347 Op<1>() = Elt;
1348 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001349 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001350}
1351
Chris Lattner54865b32006-04-08 04:05:48 +00001352InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001353 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001354 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001355 : Instruction(Vec->getType(), InsertElement,
1356 OperandTraits<InsertElementInst>::op_begin(this),
1357 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001358 assert(isValidOperands(Vec, Elt, Index) &&
1359 "Invalid insertelement instruction operands!");
1360
Gabor Greif2d3024d2008-05-26 21:33:52 +00001361 Op<0>() = Vec;
1362 Op<1>() = Elt;
1363 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001364 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001365}
1366
Chris Lattner54865b32006-04-08 04:05:48 +00001367bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1368 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001369 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001370 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001371
Reid Spencerd84d35b2007-02-15 02:26:10 +00001372 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001373 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001374
Duncan Sands9dff9be2010-02-15 16:12:20 +00001375 if (!Index->getType()->isIntegerTy(32))
Dan Gohman4fe64de2009-06-14 23:30:43 +00001376 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001377 return true;
1378}
1379
1380
Robert Bocchinoca27f032006-01-17 20:07:22 +00001381//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001382// ShuffleVectorInst Implementation
1383//===----------------------------------------------------------------------===//
1384
1385ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001386 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001387 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001388: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001389 cast<VectorType>(Mask->getType())->getNumElements()),
1390 ShuffleVector,
1391 OperandTraits<ShuffleVectorInst>::op_begin(this),
1392 OperandTraits<ShuffleVectorInst>::operands(this),
1393 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001394 assert(isValidOperands(V1, V2, Mask) &&
1395 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001396 Op<0>() = V1;
1397 Op<1>() = V2;
1398 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001399 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001400}
1401
1402ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001403 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001404 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001405: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1406 cast<VectorType>(Mask->getType())->getNumElements()),
1407 ShuffleVector,
1408 OperandTraits<ShuffleVectorInst>::op_begin(this),
1409 OperandTraits<ShuffleVectorInst>::operands(this),
1410 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001411 assert(isValidOperands(V1, V2, Mask) &&
1412 "Invalid shuffle vector instruction operands!");
1413
Gabor Greif2d3024d2008-05-26 21:33:52 +00001414 Op<0>() = V1;
1415 Op<1>() = V2;
1416 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001417 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001418}
1419
Mon P Wang25f01062008-11-10 04:46:22 +00001420bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001421 const Value *Mask) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001422 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001423 return false;
1424
1425 const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
1426 if (!isa<Constant>(Mask) || MaskTy == 0 ||
Duncan Sands9dff9be2010-02-15 16:12:20 +00001427 !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001428 return false;
1429 return true;
1430}
1431
Chris Lattnerf724e342008-03-02 05:28:33 +00001432/// getMaskValue - Return the index from the shuffle mask for the specified
1433/// output result. This is either -1 if the element is undef or a number less
1434/// than 2*numelements.
1435int ShuffleVectorInst::getMaskValue(unsigned i) const {
1436 const Constant *Mask = cast<Constant>(getOperand(2));
1437 if (isa<UndefValue>(Mask)) return -1;
1438 if (isa<ConstantAggregateZero>(Mask)) return 0;
1439 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1440 assert(i < MaskCV->getNumOperands() && "Index out of range");
1441
1442 if (isa<UndefValue>(MaskCV->getOperand(i)))
1443 return -1;
1444 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1445}
1446
Dan Gohman12fce772008-05-15 19:50:34 +00001447//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001448// InsertValueInst Class
1449//===----------------------------------------------------------------------===//
1450
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001451void InsertValueInst::init(Value *Agg, Value *Val, const unsigned *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001452 unsigned NumIdx, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001453 assert(NumOperands == 2 && "NumOperands not initialized?");
1454 Op<0>() = Agg;
1455 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001456
Dan Gohman1ecaf452008-05-31 00:58:22 +00001457 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001458 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001459}
1460
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001461void InsertValueInst::init(Value *Agg, Value *Val, unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001462 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001463 assert(NumOperands == 2 && "NumOperands not initialized?");
1464 Op<0>() = Agg;
1465 Op<1>() = Val;
1466
1467 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001468 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001469}
1470
1471InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001472 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001473 OperandTraits<InsertValueInst>::op_begin(this), 2),
1474 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001475 Op<0>() = IVI.getOperand(0);
1476 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001477 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001478}
1479
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001480InsertValueInst::InsertValueInst(Value *Agg,
1481 Value *Val,
1482 unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001483 const Twine &Name,
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001484 Instruction *InsertBefore)
1485 : Instruction(Agg->getType(), InsertValue,
1486 OperandTraits<InsertValueInst>::op_begin(this),
1487 2, InsertBefore) {
1488 init(Agg, Val, Idx, Name);
1489}
1490
1491InsertValueInst::InsertValueInst(Value *Agg,
1492 Value *Val,
1493 unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001494 const Twine &Name,
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001495 BasicBlock *InsertAtEnd)
1496 : Instruction(Agg->getType(), InsertValue,
1497 OperandTraits<InsertValueInst>::op_begin(this),
1498 2, InsertAtEnd) {
1499 init(Agg, Val, Idx, Name);
1500}
1501
Dan Gohman0752bff2008-05-23 00:36:11 +00001502//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001503// ExtractValueInst Class
1504//===----------------------------------------------------------------------===//
1505
Gabor Greifcbcc4952008-06-06 21:06:32 +00001506void ExtractValueInst::init(const unsigned *Idx, unsigned NumIdx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001507 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001508 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001509
Dan Gohman1ecaf452008-05-31 00:58:22 +00001510 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001511 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001512}
1513
Daniel Dunbar4975db62009-07-25 04:41:11 +00001514void ExtractValueInst::init(unsigned Idx, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001515 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001516
1517 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001518 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001519}
1520
1521ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001522 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001523 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001524 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001525}
1526
Dan Gohman12fce772008-05-15 19:50:34 +00001527// getIndexedType - Returns the type of the element that would be extracted
1528// with an extractvalue instruction with the specified parameters.
1529//
1530// A null type is returned if the indices are invalid for the specified
1531// pointer type.
1532//
1533const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001534 const unsigned *Idxs,
Dan Gohman12fce772008-05-15 19:50:34 +00001535 unsigned NumIdx) {
1536 unsigned CurIdx = 0;
1537 for (; CurIdx != NumIdx; ++CurIdx) {
1538 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
Duncan Sands19d0b472010-02-16 11:11:14 +00001539 if (!CT || CT->isPointerTy() || CT->isVectorTy()) return 0;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001540 unsigned Index = Idxs[CurIdx];
Dan Gohman12fce772008-05-15 19:50:34 +00001541 if (!CT->indexValid(Index)) return 0;
1542 Agg = CT->getTypeAtIndex(Index);
1543
1544 // If the new type forwards to another type, then it is in the middle
1545 // of being refined to another type (and hence, may have dropped all
1546 // references to what it was using before). So, use the new forwarded
1547 // type.
1548 if (const Type *Ty = Agg->getForwardedType())
1549 Agg = Ty;
1550 }
1551 return CurIdx == NumIdx ? Agg : 0;
1552}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001553
Dan Gohman4a3125b2008-06-17 21:07:55 +00001554const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohmand87e8132008-06-20 00:47:44 +00001555 unsigned Idx) {
1556 return getIndexedType(Agg, &Idx, 1);
Dan Gohman4a3125b2008-06-17 21:07:55 +00001557}
1558
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001559//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001560// BinaryOperator Class
1561//===----------------------------------------------------------------------===//
1562
Dan Gohmana5b96452009-06-04 22:49:04 +00001563/// AdjustIType - Map Add, Sub, and Mul to FAdd, FSub, and FMul when the
1564/// type is floating-point, to help provide compatibility with an older API.
1565///
1566static BinaryOperator::BinaryOps AdjustIType(BinaryOperator::BinaryOps iType,
1567 const Type *Ty) {
1568 // API compatibility: Adjust integer opcodes to floating-point opcodes.
Duncan Sands9dff9be2010-02-15 16:12:20 +00001569 if (Ty->isFPOrFPVectorTy()) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001570 if (iType == BinaryOperator::Add) iType = BinaryOperator::FAdd;
1571 else if (iType == BinaryOperator::Sub) iType = BinaryOperator::FSub;
1572 else if (iType == BinaryOperator::Mul) iType = BinaryOperator::FMul;
1573 }
1574 return iType;
1575}
1576
Chris Lattner2195fc42007-02-24 00:55:48 +00001577BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001578 const Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001579 Instruction *InsertBefore)
Dan Gohmana5b96452009-06-04 22:49:04 +00001580 : Instruction(Ty, AdjustIType(iType, Ty),
Gabor Greiff6caff662008-05-10 08:32:32 +00001581 OperandTraits<BinaryOperator>::op_begin(this),
1582 OperandTraits<BinaryOperator>::operands(this),
1583 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001584 Op<0>() = S1;
1585 Op<1>() = S2;
Dan Gohmana5b96452009-06-04 22:49:04 +00001586 init(AdjustIType(iType, Ty));
Chris Lattner2195fc42007-02-24 00:55:48 +00001587 setName(Name);
1588}
1589
1590BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001591 const Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001592 BasicBlock *InsertAtEnd)
Dan Gohmana5b96452009-06-04 22:49:04 +00001593 : Instruction(Ty, AdjustIType(iType, Ty),
Gabor Greiff6caff662008-05-10 08:32:32 +00001594 OperandTraits<BinaryOperator>::op_begin(this),
1595 OperandTraits<BinaryOperator>::operands(this),
1596 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001597 Op<0>() = S1;
1598 Op<1>() = S2;
Dan Gohmana5b96452009-06-04 22:49:04 +00001599 init(AdjustIType(iType, Ty));
Chris Lattner2195fc42007-02-24 00:55:48 +00001600 setName(Name);
1601}
1602
1603
1604void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001605 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001606 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001607 assert(LHS->getType() == RHS->getType() &&
1608 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001609#ifndef NDEBUG
1610 switch (iType) {
1611 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001612 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001613 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001614 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001615 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001616 "Tried to create an integer operation on a non-integer type!");
1617 break;
1618 case FAdd: case FSub:
1619 case FMul:
1620 assert(getType() == LHS->getType() &&
1621 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001622 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001623 "Tried to create a floating-point operation on a "
1624 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001625 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001626 case UDiv:
1627 case SDiv:
1628 assert(getType() == LHS->getType() &&
1629 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001630 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001631 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001632 "Incorrect operand type (not integer) for S/UDIV");
1633 break;
1634 case FDiv:
1635 assert(getType() == LHS->getType() &&
1636 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001637 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001638 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001639 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001640 case URem:
1641 case SRem:
1642 assert(getType() == LHS->getType() &&
1643 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001644 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001645 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001646 "Incorrect operand type (not integer) for S/UREM");
1647 break;
1648 case FRem:
1649 assert(getType() == LHS->getType() &&
1650 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001651 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001652 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001653 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001654 case Shl:
1655 case LShr:
1656 case AShr:
1657 assert(getType() == LHS->getType() &&
1658 "Shift operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001659 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001660 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001661 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001662 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001663 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001664 case And: case Or:
1665 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001666 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001667 "Logical operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001668 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001669 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001670 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001671 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001672 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001673 default:
1674 break;
1675 }
1676#endif
1677}
1678
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001679BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001680 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001681 Instruction *InsertBefore) {
1682 assert(S1->getType() == S2->getType() &&
1683 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001684 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001685}
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 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001690 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001691 InsertAtEnd->getInstList().push_back(Res);
1692 return Res;
1693}
1694
Dan Gohman5476cfd2009-08-12 16:23:25 +00001695BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001696 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001697 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001698 return new BinaryOperator(Instruction::Sub,
1699 zero, Op,
1700 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001701}
1702
Dan Gohman5476cfd2009-08-12 16:23:25 +00001703BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001704 BasicBlock *InsertAtEnd) {
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, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001709}
1710
Dan Gohman4ab44202009-12-18 02:58:50 +00001711BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1712 Instruction *InsertBefore) {
1713 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1714 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
1715}
1716
1717BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1718 BasicBlock *InsertAtEnd) {
1719 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1720 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
1721}
1722
Duncan Sandsfa5f5962010-02-02 12:53:04 +00001723BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1724 Instruction *InsertBefore) {
1725 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1726 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
1727}
1728
1729BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1730 BasicBlock *InsertAtEnd) {
1731 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1732 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
1733}
1734
Dan Gohman5476cfd2009-08-12 16:23:25 +00001735BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001736 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001737 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001738 return new BinaryOperator(Instruction::FSub,
1739 zero, Op,
1740 Op->getType(), Name, InsertBefore);
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 BasicBlock *InsertAtEnd) {
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, InsertAtEnd);
1749}
1750
Dan Gohman5476cfd2009-08-12 16:23:25 +00001751BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001752 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001753 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001754 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001755 C = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001756 C = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001757 std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001758 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001759 C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001760 }
1761
1762 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001763 Op->getType(), Name, InsertBefore);
1764}
1765
Dan Gohman5476cfd2009-08-12 16:23:25 +00001766BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001767 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001768 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001769 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001770 // Create a vector of all ones values.
Owen Anderson5a1acd92009-07-31 20:28:14 +00001771 Constant *Elt = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001772 AllOnes = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001773 std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001774 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001775 AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001776 }
1777
1778 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001779 Op->getType(), Name, InsertAtEnd);
1780}
1781
1782
1783// isConstantAllOnes - Helper function for several functions below
1784static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001785 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1786 return CI->isAllOnesValue();
1787 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1788 return CV->isAllOnesValue();
1789 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001790}
1791
Owen Andersonbb2501b2009-07-13 22:18:28 +00001792bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001793 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1794 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001795 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1796 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001797 return false;
1798}
1799
Owen Andersonbb2501b2009-07-13 22:18:28 +00001800bool BinaryOperator::isFNeg(const Value *V) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001801 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1802 if (Bop->getOpcode() == Instruction::FSub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001803 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
Dan Gohman11ff5702009-09-11 00:05:10 +00001804 return C->isNegativeZeroValue();
Dan Gohmana5b96452009-06-04 22:49:04 +00001805 return false;
1806}
1807
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001808bool BinaryOperator::isNot(const Value *V) {
1809 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1810 return (Bop->getOpcode() == Instruction::Xor &&
1811 (isConstantAllOnes(Bop->getOperand(1)) ||
1812 isConstantAllOnes(Bop->getOperand(0))));
1813 return false;
1814}
1815
Chris Lattner2c7d1772005-04-24 07:28:37 +00001816Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00001817 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001818}
1819
Chris Lattner2c7d1772005-04-24 07:28:37 +00001820const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1821 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001822}
1823
Dan Gohmana5b96452009-06-04 22:49:04 +00001824Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001825 return cast<BinaryOperator>(BinOp)->getOperand(1);
1826}
1827
1828const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
1829 return getFNegArgument(const_cast<Value*>(BinOp));
1830}
1831
Chris Lattner2c7d1772005-04-24 07:28:37 +00001832Value *BinaryOperator::getNotArgument(Value *BinOp) {
1833 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1834 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1835 Value *Op0 = BO->getOperand(0);
1836 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001837 if (isConstantAllOnes(Op0)) return Op1;
1838
1839 assert(isConstantAllOnes(Op1));
1840 return Op0;
1841}
1842
Chris Lattner2c7d1772005-04-24 07:28:37 +00001843const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1844 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001845}
1846
1847
1848// swapOperands - Exchange the two operands to this instruction. This
1849// instruction is safe to use on any binary instruction and does not
1850// modify the semantics of the instruction. If the instruction is
1851// order dependent (SetLT f.e.) the opcode is changed.
1852//
1853bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001854 if (!isCommutative())
1855 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001856 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001857 return false;
1858}
1859
Dan Gohman1b849082009-09-07 23:54:19 +00001860void BinaryOperator::setHasNoUnsignedWrap(bool b) {
1861 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
1862}
1863
1864void BinaryOperator::setHasNoSignedWrap(bool b) {
1865 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
1866}
1867
1868void BinaryOperator::setIsExact(bool b) {
1869 cast<SDivOperator>(this)->setIsExact(b);
1870}
1871
Nick Lewycky28a5f252009-09-27 21:33:04 +00001872bool BinaryOperator::hasNoUnsignedWrap() const {
1873 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
1874}
1875
1876bool BinaryOperator::hasNoSignedWrap() const {
1877 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
1878}
1879
1880bool BinaryOperator::isExact() const {
1881 return cast<SDivOperator>(this)->isExact();
1882}
1883
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001884//===----------------------------------------------------------------------===//
1885// CastInst Class
1886//===----------------------------------------------------------------------===//
1887
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001888// Just determine if this cast only deals with integral->integral conversion.
1889bool CastInst::isIntegerCast() const {
1890 switch (getOpcode()) {
1891 default: return false;
1892 case Instruction::ZExt:
1893 case Instruction::SExt:
1894 case Instruction::Trunc:
1895 return true;
1896 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00001897 return getOperand(0)->getType()->isIntegerTy() &&
1898 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001899 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001900}
1901
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001902bool CastInst::isLosslessCast() const {
1903 // Only BitCast can be lossless, exit fast if we're not BitCast
1904 if (getOpcode() != Instruction::BitCast)
1905 return false;
1906
1907 // Identity cast is always lossless
1908 const Type* SrcTy = getOperand(0)->getType();
1909 const Type* DstTy = getType();
1910 if (SrcTy == DstTy)
1911 return true;
1912
Reid Spencer8d9336d2006-12-31 05:26:44 +00001913 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00001914 if (SrcTy->isPointerTy())
1915 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001916 return false; // Other types have no identity values
1917}
1918
1919/// This function determines if the CastInst does not require any bits to be
1920/// changed in order to effect the cast. Essentially, it identifies cases where
1921/// no code gen is necessary for the cast, hence the name no-op cast. For
1922/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00001923/// # bitcast i32* %x to i8*
1924/// # bitcast <2 x i32> %x to <4 x i16>
1925/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001926/// @brief Determine if a cast is a no-op.
1927bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1928 switch (getOpcode()) {
1929 default:
1930 assert(!"Invalid CastOp");
1931 case Instruction::Trunc:
1932 case Instruction::ZExt:
1933 case Instruction::SExt:
1934 case Instruction::FPTrunc:
1935 case Instruction::FPExt:
1936 case Instruction::UIToFP:
1937 case Instruction::SIToFP:
1938 case Instruction::FPToUI:
1939 case Instruction::FPToSI:
1940 return false; // These always modify bits
1941 case Instruction::BitCast:
1942 return true; // BitCast never modifies bits.
1943 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001944 return IntPtrTy->getScalarSizeInBits() ==
1945 getType()->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001946 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001947 return IntPtrTy->getScalarSizeInBits() ==
1948 getOperand(0)->getType()->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001949 }
1950}
1951
1952/// This function determines if a pair of casts can be eliminated and what
1953/// opcode should be used in the elimination. This assumes that there are two
1954/// instructions like this:
1955/// * %F = firstOpcode SrcTy %x to MidTy
1956/// * %S = secondOpcode MidTy %F to DstTy
1957/// The function returns a resultOpcode so these two casts can be replaced with:
1958/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1959/// If no such cast is permited, the function returns 0.
1960unsigned CastInst::isEliminableCastPair(
1961 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1962 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1963{
1964 // Define the 144 possibilities for these two cast instructions. The values
1965 // in this matrix determine what to do in a given situation and select the
1966 // case in the switch below. The rows correspond to firstOp, the columns
1967 // correspond to secondOp. In looking at the table below, keep in mind
1968 // the following cast properties:
1969 //
1970 // Size Compare Source Destination
1971 // Operator Src ? Size Type Sign Type Sign
1972 // -------- ------------ ------------------- ---------------------
1973 // TRUNC > Integer Any Integral Any
1974 // ZEXT < Integral Unsigned Integer Any
1975 // SEXT < Integral Signed Integer Any
1976 // FPTOUI n/a FloatPt n/a Integral Unsigned
1977 // FPTOSI n/a FloatPt n/a Integral Signed
1978 // UITOFP n/a Integral Unsigned FloatPt n/a
1979 // SITOFP n/a Integral Signed FloatPt n/a
1980 // FPTRUNC > FloatPt n/a FloatPt n/a
1981 // FPEXT < FloatPt n/a FloatPt n/a
1982 // PTRTOINT n/a Pointer n/a Integral Unsigned
1983 // INTTOPTR n/a Integral Unsigned Pointer n/a
Dan Gohmaneb7111b2010-04-07 23:22:42 +00001984 // BITCAST = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001985 //
1986 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00001987 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
1988 // into "fptoui double to i64", but this loses information about the range
Chris Lattner6f6b4972006-12-05 23:43:59 +00001989 // of the produced value (we no longer know the top-part is all zeros).
1990 // Further this conversion is often much more expensive for typical hardware,
1991 // and causes issues when building libgcc. We disallow fptosi+sext for the
1992 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001993 const unsigned numCastOps =
1994 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1995 static const uint8_t CastResults[numCastOps][numCastOps] = {
1996 // T F F U S F F P I B -+
1997 // R Z S P P I I T P 2 N T |
1998 // U E E 2 2 2 2 R E I T C +- secondOp
1999 // N X X U S F F N X N 2 V |
2000 // C T T I I P P C T T P T -+
2001 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
2002 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
2003 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00002004 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
2005 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002006 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
2007 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
2008 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
2009 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
2010 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
2011 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
2012 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
2013 };
2014
2015 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2016 [secondOp-Instruction::CastOpsBegin];
2017 switch (ElimCase) {
2018 case 0:
2019 // categorically disallowed
2020 return 0;
2021 case 1:
2022 // allowed, use first cast's opcode
2023 return firstOp;
2024 case 2:
2025 // allowed, use second cast's opcode
2026 return secondOp;
2027 case 3:
2028 // no-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002029 // is integer and we are not converting between a vector and a
Chris Lattner531732b2010-01-23 04:42:42 +00002030 // non vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002031 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002032 return firstOp;
2033 return 0;
2034 case 4:
2035 // no-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002036 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002037 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002038 return firstOp;
2039 return 0;
2040 case 5:
2041 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002042 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002043 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002044 return secondOp;
2045 return 0;
2046 case 6:
2047 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002048 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002049 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002050 return secondOp;
2051 return 0;
2052 case 7: {
2053 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
Dan Gohman9413de12009-07-21 23:19:40 +00002054 if (!IntPtrTy)
2055 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002056 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2057 unsigned MidSize = MidTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002058 if (MidSize >= PtrSize)
2059 return Instruction::BitCast;
2060 return 0;
2061 }
2062 case 8: {
2063 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2064 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2065 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002066 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2067 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002068 if (SrcSize == DstSize)
2069 return Instruction::BitCast;
2070 else if (SrcSize < DstSize)
2071 return firstOp;
2072 return secondOp;
2073 }
2074 case 9: // zext, sext -> zext, because sext can't sign extend after zext
2075 return Instruction::ZExt;
2076 case 10:
2077 // fpext followed by ftrunc is allowed if the bit size returned to is
2078 // the same as the original, in which case its just a bitcast
2079 if (SrcTy == DstTy)
2080 return Instruction::BitCast;
2081 return 0; // If the types are not the same we can't eliminate it.
2082 case 11:
2083 // bitcast followed by ptrtoint is allowed as long as the bitcast
2084 // is a pointer to pointer cast.
Duncan Sands19d0b472010-02-16 11:11:14 +00002085 if (SrcTy->isPointerTy() && MidTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002086 return secondOp;
2087 return 0;
2088 case 12:
2089 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
Duncan Sands19d0b472010-02-16 11:11:14 +00002090 if (MidTy->isPointerTy() && DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002091 return firstOp;
2092 return 0;
2093 case 13: {
2094 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Dan Gohman9413de12009-07-21 23:19:40 +00002095 if (!IntPtrTy)
2096 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002097 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2098 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2099 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002100 if (SrcSize <= PtrSize && SrcSize == DstSize)
2101 return Instruction::BitCast;
2102 return 0;
2103 }
2104 case 99:
2105 // cast combination can't happen (error in input). This is for all cases
2106 // where the MidTy is not the same for the two cast instructions.
2107 assert(!"Invalid Cast Combination");
2108 return 0;
2109 default:
2110 assert(!"Error in CastResults table!!!");
2111 return 0;
2112 }
2113 return 0;
2114}
2115
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002116CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002117 const Twine &Name, Instruction *InsertBefore) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002118 // Construct and return the appropriate CastInst subclass
2119 switch (op) {
2120 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2121 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2122 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2123 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2124 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2125 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2126 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2127 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2128 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2129 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2130 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2131 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2132 default:
2133 assert(!"Invalid opcode provided");
2134 }
2135 return 0;
2136}
2137
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002138CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002139 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002140 // Construct and return the appropriate CastInst subclass
2141 switch (op) {
2142 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2143 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2144 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2145 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2146 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2147 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2148 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2149 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2150 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2151 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2152 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2153 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2154 default:
2155 assert(!"Invalid opcode provided");
2156 }
2157 return 0;
2158}
2159
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002160CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002161 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002162 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002163 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002164 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2165 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002166}
2167
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002168CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002169 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002170 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002171 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002172 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2173 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002174}
2175
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002176CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002177 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002178 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002179 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002180 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2181 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002182}
2183
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002184CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002185 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002186 BasicBlock *InsertAtEnd) {
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, InsertAtEnd);
2189 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002190}
2191
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002192CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002193 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002194 Instruction *InsertBefore) {
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, InsertBefore);
2197 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002198}
2199
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002200CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002201 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002202 BasicBlock *InsertAtEnd) {
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, InsertAtEnd);
2205 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002206}
2207
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002208CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002209 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002210 BasicBlock *InsertAtEnd) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002211 assert(S->getType()->isPointerTy() && "Invalid cast");
2212 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002213 "Invalid cast");
2214
Duncan Sands9dff9be2010-02-15 16:12:20 +00002215 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002216 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2217 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002218}
2219
2220/// @brief Create a BitCast or a PtrToInt cast instruction
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002221CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002222 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002223 Instruction *InsertBefore) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002224 assert(S->getType()->isPointerTy() && "Invalid cast");
2225 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002226 "Invalid cast");
2227
Duncan Sands9dff9be2010-02-15 16:12:20 +00002228 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002229 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2230 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002231}
2232
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002233CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002234 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002235 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002236 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002237 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002238 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2239 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002240 Instruction::CastOps opcode =
2241 (SrcBits == DstBits ? Instruction::BitCast :
2242 (SrcBits > DstBits ? Instruction::Trunc :
2243 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002244 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002245}
2246
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002247CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002248 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002249 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002250 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002251 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002252 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2253 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002254 Instruction::CastOps opcode =
2255 (SrcBits == DstBits ? Instruction::BitCast :
2256 (SrcBits > DstBits ? Instruction::Trunc :
2257 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002258 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002259}
2260
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002261CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002262 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002263 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002264 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002265 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002266 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2267 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002268 Instruction::CastOps opcode =
2269 (SrcBits == DstBits ? Instruction::BitCast :
2270 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002271 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002272}
2273
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002274CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002275 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002276 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002277 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002278 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002279 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2280 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002281 Instruction::CastOps opcode =
2282 (SrcBits == DstBits ? Instruction::BitCast :
2283 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002284 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002285}
2286
Duncan Sands55e50902008-01-06 10:12:28 +00002287// Check whether it is valid to call getCastOpcode for these types.
2288// This routine must be kept in sync with getCastOpcode.
2289bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
2290 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2291 return false;
2292
2293 if (SrcTy == DestTy)
2294 return true;
2295
2296 // Get the bit sizes, we'll need these
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002297 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2298 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Duncan Sands55e50902008-01-06 10:12:28 +00002299
2300 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002301 if (DestTy->isIntegerTy()) { // Casting to integral
2302 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002303 return true;
Duncan Sands9dff9be2010-02-15 16:12:20 +00002304 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002305 return true;
2306 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002307 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002308 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002309 } else { // Casting from something else
Duncan Sands19d0b472010-02-16 11:11:14 +00002310 return SrcTy->isPointerTy();
Duncan Sands55e50902008-01-06 10:12:28 +00002311 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002312 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2313 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002314 return true;
Duncan Sands9dff9be2010-02-15 16:12:20 +00002315 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002316 return true;
2317 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002318 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002319 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002320 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002321 return false;
2322 }
2323 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002324 // Casting to vector
Duncan Sands55e50902008-01-06 10:12:28 +00002325 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002326 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002327 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002328 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002329 return DestPTy->getBitWidth() == SrcBits;
2330 }
Duncan Sands19d0b472010-02-16 11:11:14 +00002331 } else if (DestTy->isPointerTy()) { // Casting to pointer
2332 if (SrcTy->isPointerTy()) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002333 return true;
Duncan Sands9dff9be2010-02-15 16:12:20 +00002334 } else if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002335 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002336 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002337 return false;
2338 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002339 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002340 return false;
2341 }
2342}
2343
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002344// Provide a way to get a "cast" where the cast opcode is inferred from the
2345// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002346// logic in the castIsValid function below. This axiom should hold:
2347// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2348// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002349// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002350// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002351Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002352CastInst::getCastOpcode(
2353 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002354 // Get the bit sizes, we'll need these
2355 const Type *SrcTy = Src->getType();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002356 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2357 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002358
Duncan Sands55e50902008-01-06 10:12:28 +00002359 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2360 "Only first class types are castable!");
2361
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002362 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002363 if (DestTy->isIntegerTy()) { // Casting to integral
2364 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002365 if (DestBits < SrcBits)
2366 return Trunc; // int -> smaller int
2367 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002368 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002369 return SExt; // signed -> SEXT
2370 else
2371 return ZExt; // unsigned -> ZEXT
2372 } else {
2373 return BitCast; // Same size, No-op cast
2374 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002375 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002376 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002377 return FPToSI; // FP -> sint
2378 else
2379 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00002380 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002381 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002382 "Casting vector to integer of different width");
Devang Patele9432132008-11-05 01:37:40 +00002383 PTy = NULL;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002384 return BitCast; // Same size, no-op cast
2385 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00002386 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002387 "Casting from a value that is not first-class type");
2388 return PtrToInt; // ptr -> int
2389 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002390 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2391 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002392 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002393 return SIToFP; // sint -> FP
2394 else
2395 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00002396 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002397 if (DestBits < SrcBits) {
2398 return FPTrunc; // FP -> smaller FP
2399 } else if (DestBits > SrcBits) {
2400 return FPExt; // FP -> larger FP
2401 } else {
2402 return BitCast; // same size, no-op cast
2403 }
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 floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002407 PTy = NULL;
2408 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002409 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002410 llvm_unreachable("Casting pointer or non-first class to float");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002411 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002412 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2413 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002414 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002415 "Casting vector to vector of different widths");
Devang Patelcb181bb2008-11-21 20:00:59 +00002416 SrcPTy = NULL;
Dan Gohmanfead7972007-05-11 21:43:24 +00002417 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002418 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00002419 return BitCast; // float/int -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002420 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00002421 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002422 }
Duncan Sands19d0b472010-02-16 11:11:14 +00002423 } else if (DestTy->isPointerTy()) {
2424 if (SrcTy->isPointerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002425 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00002426 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002427 return IntToPtr; // int -> ptr
2428 } else {
2429 assert(!"Casting pointer to other than pointer or int");
2430 }
2431 } else {
2432 assert(!"Casting to type that is not first-class");
2433 }
2434
2435 // If we fall through to here we probably hit an assertion cast above
2436 // and assertions are not turned on. Anything we return is an error, so
2437 // BitCast is as good a choice as any.
2438 return BitCast;
2439}
2440
2441//===----------------------------------------------------------------------===//
2442// CastInst SubClass Constructors
2443//===----------------------------------------------------------------------===//
2444
2445/// Check that the construction parameters for a CastInst are correct. This
2446/// could be broken out into the separate constructors but it is useful to have
2447/// it in one place and to eliminate the redundant code for getting the sizes
2448/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002449bool
2450CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002451
2452 // Check for type sanity on the arguments
2453 const Type *SrcTy = S->getType();
Chris Lattner37bc78a2010-01-26 21:51:43 +00002454 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
2455 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002456 return false;
2457
2458 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002459 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2460 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002461
2462 // Switch on the opcode provided
2463 switch (op) {
2464 default: return false; // This is an input error
2465 case Instruction::Trunc:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002466 return SrcTy->isIntOrIntVectorTy() &&
2467 DstTy->isIntOrIntVectorTy()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002468 case Instruction::ZExt:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002469 return SrcTy->isIntOrIntVectorTy() &&
2470 DstTy->isIntOrIntVectorTy()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002471 case Instruction::SExt:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002472 return SrcTy->isIntOrIntVectorTy() &&
2473 DstTy->isIntOrIntVectorTy()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002474 case Instruction::FPTrunc:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002475 return SrcTy->isFPOrFPVectorTy() &&
2476 DstTy->isFPOrFPVectorTy() &&
Dan Gohman550c9af2008-08-14 20:04:46 +00002477 SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002478 case Instruction::FPExt:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002479 return SrcTy->isFPOrFPVectorTy() &&
2480 DstTy->isFPOrFPVectorTy() &&
Dan Gohman550c9af2008-08-14 20:04:46 +00002481 SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002482 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002483 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00002484 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2485 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002486 return SVTy->getElementType()->isIntOrIntVectorTy() &&
2487 DVTy->getElementType()->isFPOrFPVectorTy() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002488 SVTy->getNumElements() == DVTy->getNumElements();
2489 }
2490 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002491 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002492 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002493 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00002494 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2495 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002496 return SVTy->getElementType()->isFPOrFPVectorTy() &&
2497 DVTy->getElementType()->isIntOrIntVectorTy() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002498 SVTy->getNumElements() == DVTy->getNumElements();
2499 }
2500 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002501 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002502 case Instruction::PtrToInt:
Duncan Sands19d0b472010-02-16 11:11:14 +00002503 return SrcTy->isPointerTy() && DstTy->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002504 case Instruction::IntToPtr:
Duncan Sands19d0b472010-02-16 11:11:14 +00002505 return SrcTy->isIntegerTy() && DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002506 case Instruction::BitCast:
2507 // BitCast implies a no-op cast of type only. No bits change.
2508 // However, you can't cast pointers to anything but pointers.
Duncan Sands19d0b472010-02-16 11:11:14 +00002509 if (SrcTy->isPointerTy() != DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002510 return false;
2511
Duncan Sands55e50902008-01-06 10:12:28 +00002512 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002513 // these cases, the cast is okay if the source and destination bit widths
2514 // are identical.
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002515 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002516 }
2517}
2518
2519TruncInst::TruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002520 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002521) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002522 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002523}
2524
2525TruncInst::TruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002526 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002527) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002528 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002529}
2530
2531ZExtInst::ZExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002532 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002533) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002534 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002535}
2536
2537ZExtInst::ZExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002538 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002539) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002540 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002541}
2542SExtInst::SExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002543 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002544) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002545 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002546}
2547
Jeff Cohencc08c832006-12-02 02:22:01 +00002548SExtInst::SExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002549 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002550) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002551 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002552}
2553
2554FPTruncInst::FPTruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002555 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002556) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002557 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002558}
2559
2560FPTruncInst::FPTruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002561 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002562) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002563 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002564}
2565
2566FPExtInst::FPExtInst(
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, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002569 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002570}
2571
2572FPExtInst::FPExtInst(
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, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002575 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002576}
2577
2578UIToFPInst::UIToFPInst(
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, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002581 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002582}
2583
2584UIToFPInst::UIToFPInst(
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, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002587 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002588}
2589
2590SIToFPInst::SIToFPInst(
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, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002593 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002594}
2595
2596SIToFPInst::SIToFPInst(
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, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002599 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002600}
2601
2602FPToUIInst::FPToUIInst(
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, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002605 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002606}
2607
2608FPToUIInst::FPToUIInst(
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, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002611 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002612}
2613
2614FPToSIInst::FPToSIInst(
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, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002617 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002618}
2619
2620FPToSIInst::FPToSIInst(
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, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002623 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002624}
2625
2626PtrToIntInst::PtrToIntInst(
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, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002629 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002630}
2631
2632PtrToIntInst::PtrToIntInst(
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, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002635 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002636}
2637
2638IntToPtrInst::IntToPtrInst(
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, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002641 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002642}
2643
2644IntToPtrInst::IntToPtrInst(
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, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002647 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002648}
2649
2650BitCastInst::BitCastInst(
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, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002653 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002654}
2655
2656BitCastInst::BitCastInst(
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, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002659 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002660}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002661
2662//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002663// CmpInst Classes
2664//===----------------------------------------------------------------------===//
2665
Chris Lattneraec33da2010-01-22 06:25:37 +00002666void CmpInst::Anchor() const {}
2667
Nate Begemand2195702008-05-12 19:01:56 +00002668CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002669 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002670 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002671 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002672 OperandTraits<CmpInst>::op_begin(this),
2673 OperandTraits<CmpInst>::operands(this),
2674 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002675 Op<0>() = LHS;
2676 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002677 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002678 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002679}
Gabor Greiff6caff662008-05-10 08:32:32 +00002680
Nate Begemand2195702008-05-12 19:01:56 +00002681CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002682 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002683 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002684 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002685 OperandTraits<CmpInst>::op_begin(this),
2686 OperandTraits<CmpInst>::operands(this),
2687 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002688 Op<0>() = LHS;
2689 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002690 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002691 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002692}
2693
2694CmpInst *
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002695CmpInst::Create(OtherOps Op, unsigned short predicate,
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002696 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002697 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002698 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002699 if (InsertBefore)
2700 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
2701 S1, S2, Name);
2702 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002703 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002704 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002705 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002706
2707 if (InsertBefore)
2708 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
2709 S1, S2, Name);
2710 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002711 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002712 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002713}
2714
2715CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002716CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002717 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002718 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002719 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2720 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002721 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002722 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2723 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002724}
2725
2726void CmpInst::swapOperands() {
2727 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2728 IC->swapOperands();
2729 else
2730 cast<FCmpInst>(this)->swapOperands();
2731}
2732
2733bool CmpInst::isCommutative() {
2734 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2735 return IC->isCommutative();
2736 return cast<FCmpInst>(this)->isCommutative();
2737}
2738
2739bool CmpInst::isEquality() {
2740 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2741 return IC->isEquality();
2742 return cast<FCmpInst>(this)->isEquality();
2743}
2744
2745
Dan Gohman4e724382008-05-31 02:47:54 +00002746CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002747 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002748 default: assert(!"Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002749 case ICMP_EQ: return ICMP_NE;
2750 case ICMP_NE: return ICMP_EQ;
2751 case ICMP_UGT: return ICMP_ULE;
2752 case ICMP_ULT: return ICMP_UGE;
2753 case ICMP_UGE: return ICMP_ULT;
2754 case ICMP_ULE: return ICMP_UGT;
2755 case ICMP_SGT: return ICMP_SLE;
2756 case ICMP_SLT: return ICMP_SGE;
2757 case ICMP_SGE: return ICMP_SLT;
2758 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00002759
Dan Gohman4e724382008-05-31 02:47:54 +00002760 case FCMP_OEQ: return FCMP_UNE;
2761 case FCMP_ONE: return FCMP_UEQ;
2762 case FCMP_OGT: return FCMP_ULE;
2763 case FCMP_OLT: return FCMP_UGE;
2764 case FCMP_OGE: return FCMP_ULT;
2765 case FCMP_OLE: return FCMP_UGT;
2766 case FCMP_UEQ: return FCMP_ONE;
2767 case FCMP_UNE: return FCMP_OEQ;
2768 case FCMP_UGT: return FCMP_OLE;
2769 case FCMP_ULT: return FCMP_OGE;
2770 case FCMP_UGE: return FCMP_OLT;
2771 case FCMP_ULE: return FCMP_OGT;
2772 case FCMP_ORD: return FCMP_UNO;
2773 case FCMP_UNO: return FCMP_ORD;
2774 case FCMP_TRUE: return FCMP_FALSE;
2775 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00002776 }
2777}
2778
Reid Spencer266e42b2006-12-23 06:05:41 +00002779ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2780 switch (pred) {
2781 default: assert(! "Unknown icmp predicate!");
2782 case ICMP_EQ: case ICMP_NE:
2783 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2784 return pred;
2785 case ICMP_UGT: return ICMP_SGT;
2786 case ICMP_ULT: return ICMP_SLT;
2787 case ICMP_UGE: return ICMP_SGE;
2788 case ICMP_ULE: return ICMP_SLE;
2789 }
2790}
2791
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002792ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2793 switch (pred) {
2794 default: assert(! "Unknown icmp predicate!");
2795 case ICMP_EQ: case ICMP_NE:
2796 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2797 return pred;
2798 case ICMP_SGT: return ICMP_UGT;
2799 case ICMP_SLT: return ICMP_ULT;
2800 case ICMP_SGE: return ICMP_UGE;
2801 case ICMP_SLE: return ICMP_ULE;
2802 }
2803}
2804
Reid Spencer0286bc12007-02-28 22:00:54 +00002805/// Initialize a set of values that all satisfy the condition with C.
2806///
2807ConstantRange
2808ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2809 APInt Lower(C);
2810 APInt Upper(C);
2811 uint32_t BitWidth = C.getBitWidth();
2812 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002813 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Reid Spencer0286bc12007-02-28 22:00:54 +00002814 case ICmpInst::ICMP_EQ: Upper++; break;
2815 case ICmpInst::ICMP_NE: Lower++; break;
Dan Gohmand86e2952010-01-26 16:04:20 +00002816 case ICmpInst::ICMP_ULT:
2817 Lower = APInt::getMinValue(BitWidth);
2818 // Check for an empty-set condition.
2819 if (Lower == Upper)
2820 return ConstantRange(BitWidth, /*isFullSet=*/false);
2821 break;
2822 case ICmpInst::ICMP_SLT:
2823 Lower = APInt::getSignedMinValue(BitWidth);
2824 // Check for an empty-set condition.
2825 if (Lower == Upper)
2826 return ConstantRange(BitWidth, /*isFullSet=*/false);
2827 break;
Reid Spencer0286bc12007-02-28 22:00:54 +00002828 case ICmpInst::ICMP_UGT:
2829 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002830 // Check for an empty-set condition.
2831 if (Lower == Upper)
2832 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00002833 break;
2834 case ICmpInst::ICMP_SGT:
2835 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002836 // Check for an empty-set condition.
2837 if (Lower == Upper)
2838 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00002839 break;
2840 case ICmpInst::ICMP_ULE:
2841 Lower = APInt::getMinValue(BitWidth); Upper++;
Dan Gohmand86e2952010-01-26 16:04:20 +00002842 // Check for a full-set condition.
2843 if (Lower == Upper)
2844 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002845 break;
2846 case ICmpInst::ICMP_SLE:
2847 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
Dan Gohmand86e2952010-01-26 16:04:20 +00002848 // Check for a full-set condition.
2849 if (Lower == Upper)
2850 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002851 break;
2852 case ICmpInst::ICMP_UGE:
2853 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002854 // Check for a full-set condition.
2855 if (Lower == Upper)
2856 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002857 break;
2858 case ICmpInst::ICMP_SGE:
2859 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002860 // Check for a full-set condition.
2861 if (Lower == Upper)
2862 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002863 break;
2864 }
2865 return ConstantRange(Lower, Upper);
2866}
2867
Dan Gohman4e724382008-05-31 02:47:54 +00002868CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002869 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002870 default: assert(!"Unknown cmp predicate!");
2871 case ICMP_EQ: case ICMP_NE:
2872 return pred;
2873 case ICMP_SGT: return ICMP_SLT;
2874 case ICMP_SLT: return ICMP_SGT;
2875 case ICMP_SGE: return ICMP_SLE;
2876 case ICMP_SLE: return ICMP_SGE;
2877 case ICMP_UGT: return ICMP_ULT;
2878 case ICMP_ULT: return ICMP_UGT;
2879 case ICMP_UGE: return ICMP_ULE;
2880 case ICMP_ULE: return ICMP_UGE;
2881
Reid Spencerd9436b62006-11-20 01:22:35 +00002882 case FCMP_FALSE: case FCMP_TRUE:
2883 case FCMP_OEQ: case FCMP_ONE:
2884 case FCMP_UEQ: case FCMP_UNE:
2885 case FCMP_ORD: case FCMP_UNO:
2886 return pred;
2887 case FCMP_OGT: return FCMP_OLT;
2888 case FCMP_OLT: return FCMP_OGT;
2889 case FCMP_OGE: return FCMP_OLE;
2890 case FCMP_OLE: return FCMP_OGE;
2891 case FCMP_UGT: return FCMP_ULT;
2892 case FCMP_ULT: return FCMP_UGT;
2893 case FCMP_UGE: return FCMP_ULE;
2894 case FCMP_ULE: return FCMP_UGE;
2895 }
2896}
2897
Reid Spencer266e42b2006-12-23 06:05:41 +00002898bool CmpInst::isUnsigned(unsigned short predicate) {
2899 switch (predicate) {
2900 default: return false;
2901 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2902 case ICmpInst::ICMP_UGE: return true;
2903 }
2904}
2905
Nick Lewycky7494b3b2009-10-25 03:50:03 +00002906bool CmpInst::isSigned(unsigned short predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002907 switch (predicate) {
2908 default: return false;
2909 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2910 case ICmpInst::ICMP_SGE: return true;
2911 }
2912}
2913
2914bool CmpInst::isOrdered(unsigned short predicate) {
2915 switch (predicate) {
2916 default: return false;
2917 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2918 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2919 case FCmpInst::FCMP_ORD: return true;
2920 }
2921}
2922
2923bool CmpInst::isUnordered(unsigned short predicate) {
2924 switch (predicate) {
2925 default: return false;
2926 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2927 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2928 case FCmpInst::FCMP_UNO: return true;
2929 }
2930}
2931
Nick Lewycky7494b3b2009-10-25 03:50:03 +00002932bool CmpInst::isTrueWhenEqual(unsigned short predicate) {
2933 switch(predicate) {
2934 default: return false;
2935 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
2936 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
2937 }
2938}
2939
2940bool CmpInst::isFalseWhenEqual(unsigned short predicate) {
2941 switch(predicate) {
2942 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
2943 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
2944 default: return false;
2945 }
2946}
2947
2948
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002949//===----------------------------------------------------------------------===//
2950// SwitchInst Implementation
2951//===----------------------------------------------------------------------===//
2952
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002953void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002954 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002955 ReservedSpace = 2+NumCases*2;
2956 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00002957 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002958
Gabor Greif2d3024d2008-05-26 21:33:52 +00002959 OperandList[0] = Value;
2960 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002961}
2962
Chris Lattner2195fc42007-02-24 00:55:48 +00002963/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2964/// switch on and a default destination. The number of additional cases can
2965/// be specified here to make memory allocation more efficient. This
2966/// constructor can also autoinsert before another instruction.
2967SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2968 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00002969 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2970 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +00002971 init(Value, Default, NumCases);
2972}
2973
2974/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2975/// switch on and a default destination. The number of additional cases can
2976/// be specified here to make memory allocation more efficient. This
2977/// constructor also autoinserts at the end of the specified BasicBlock.
2978SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2979 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00002980 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2981 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +00002982 init(Value, Default, NumCases);
2983}
2984
Misha Brukmanb1c93172005-04-21 23:48:37 +00002985SwitchInst::SwitchInst(const SwitchInst &SI)
Owen Anderson55f1c092009-08-13 21:58:54 +00002986 : TerminatorInst(Type::getVoidTy(SI.getContext()), Instruction::Switch,
Gabor Greiff6caff662008-05-10 08:32:32 +00002987 allocHungoffUses(SI.getNumOperands()), SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002988 Use *OL = OperandList, *InOL = SI.OperandList;
2989 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002990 OL[i] = InOL[i];
2991 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002992 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002993 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002994}
2995
Gordon Henriksen14a55692007-12-10 02:14:30 +00002996SwitchInst::~SwitchInst() {
Gabor Greiff6caff662008-05-10 08:32:32 +00002997 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002998}
2999
3000
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003001/// addCase - Add an entry to the switch instruction...
3002///
Chris Lattner47ac1872005-02-24 05:32:09 +00003003void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003004 unsigned OpNo = NumOperands;
3005 if (OpNo+2 > ReservedSpace)
3006 resizeOperands(0); // Get more space!
3007 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003008 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003009 NumOperands = OpNo+2;
Gabor Greif2d3024d2008-05-26 21:33:52 +00003010 OperandList[OpNo] = OnVal;
3011 OperandList[OpNo+1] = Dest;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003012}
3013
3014/// removeCase - This method removes the specified successor from the switch
3015/// instruction. Note that this cannot be used to remove the default
3016/// destination (successor #0).
3017///
3018void SwitchInst::removeCase(unsigned idx) {
3019 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003020 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
3021
3022 unsigned NumOps = getNumOperands();
3023 Use *OL = OperandList;
3024
3025 // Move everything after this operand down.
3026 //
3027 // FIXME: we could just swap with the end of the list, then erase. However,
3028 // client might not expect this to happen. The code as it is thrashes the
3029 // use/def lists, which is kinda lame.
3030 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
3031 OL[i-2] = OL[i];
3032 OL[i-2+1] = OL[i+1];
3033 }
3034
3035 // Nuke the last value.
3036 OL[NumOps-2].set(0);
3037 OL[NumOps-2+1].set(0);
3038 NumOperands = NumOps-2;
3039}
3040
3041/// resizeOperands - resize operands - This adjusts the length of the operands
3042/// list according to the following behavior:
3043/// 1. If NumOps == 0, grow the operand list in response to a push_back style
Gabor Greiff6caff662008-05-10 08:32:32 +00003044/// of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003045/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
3046/// 3. If NumOps == NumOperands, trim the reserved space.
3047///
3048void SwitchInst::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +00003049 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003050 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +00003051 NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003052 } else if (NumOps*2 > NumOperands) {
3053 // No resize needed.
3054 if (ReservedSpace >= NumOps) return;
3055 } else if (NumOps == NumOperands) {
3056 if (ReservedSpace == NumOps) return;
3057 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003058 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003059 }
3060
3061 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00003062 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003063 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00003064 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003065 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003066 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003067 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00003068 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003069}
3070
3071
3072BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3073 return getSuccessor(idx);
3074}
3075unsigned SwitchInst::getNumSuccessorsV() const {
3076 return getNumSuccessors();
3077}
3078void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3079 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003080}
Chris Lattnerf22be932004-10-15 23:52:53 +00003081
Chris Lattner3ed871f2009-10-27 19:13:16 +00003082//===----------------------------------------------------------------------===//
3083// SwitchInst Implementation
3084//===----------------------------------------------------------------------===//
3085
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003086void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003087 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003088 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003089 ReservedSpace = 1+NumDests;
3090 NumOperands = 1;
3091 OperandList = allocHungoffUses(ReservedSpace);
3092
3093 OperandList[0] = Address;
3094}
3095
3096
3097/// resizeOperands - resize operands - This adjusts the length of the operands
3098/// list according to the following behavior:
3099/// 1. If NumOps == 0, grow the operand list in response to a push_back style
3100/// of operation. This grows the number of ops by 2 times.
3101/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
3102/// 3. If NumOps == NumOperands, trim the reserved space.
3103///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003104void IndirectBrInst::resizeOperands(unsigned NumOps) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003105 unsigned e = getNumOperands();
3106 if (NumOps == 0) {
3107 NumOps = e*2;
3108 } else if (NumOps*2 > NumOperands) {
3109 // No resize needed.
3110 if (ReservedSpace >= NumOps) return;
3111 } else if (NumOps == NumOperands) {
3112 if (ReservedSpace == NumOps) return;
3113 } else {
3114 return;
3115 }
3116
3117 ReservedSpace = NumOps;
3118 Use *NewOps = allocHungoffUses(NumOps);
3119 Use *OldOps = OperandList;
3120 for (unsigned i = 0; i != e; ++i)
3121 NewOps[i] = OldOps[i];
3122 OperandList = NewOps;
3123 if (OldOps) Use::zap(OldOps, OldOps + e, true);
3124}
3125
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003126IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3127 Instruction *InsertBefore)
3128: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003129 0, 0, InsertBefore) {
3130 init(Address, NumCases);
3131}
3132
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003133IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3134 BasicBlock *InsertAtEnd)
3135: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003136 0, 0, InsertAtEnd) {
3137 init(Address, NumCases);
3138}
3139
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003140IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
3141 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003142 allocHungoffUses(IBI.getNumOperands()),
3143 IBI.getNumOperands()) {
3144 Use *OL = OperandList, *InOL = IBI.OperandList;
3145 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3146 OL[i] = InOL[i];
3147 SubclassOptionalData = IBI.SubclassOptionalData;
3148}
3149
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003150IndirectBrInst::~IndirectBrInst() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003151 dropHungoffUses(OperandList);
3152}
3153
3154/// addDestination - Add a destination.
3155///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003156void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003157 unsigned OpNo = NumOperands;
3158 if (OpNo+1 > ReservedSpace)
3159 resizeOperands(0); // Get more space!
3160 // Initialize some new operands.
3161 assert(OpNo < ReservedSpace && "Growing didn't work!");
3162 NumOperands = OpNo+1;
3163 OperandList[OpNo] = DestBB;
3164}
3165
3166/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003167/// indirectbr instruction.
3168void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003169 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3170
3171 unsigned NumOps = getNumOperands();
3172 Use *OL = OperandList;
3173
3174 // Replace this value with the last one.
3175 OL[idx+1] = OL[NumOps-1];
3176
3177 // Nuke the last value.
3178 OL[NumOps-1].set(0);
3179 NumOperands = NumOps-1;
3180}
3181
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003182BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003183 return getSuccessor(idx);
3184}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003185unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003186 return getNumSuccessors();
3187}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003188void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003189 setSuccessor(idx, B);
3190}
3191
3192//===----------------------------------------------------------------------===//
Devang Patel11cf3f42009-10-27 22:16:29 +00003193// clone_impl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003194//===----------------------------------------------------------------------===//
3195
Chris Lattnerf22be932004-10-15 23:52:53 +00003196// Define these methods here so vtables don't get emitted into every translation
3197// unit that uses these classes.
3198
Devang Patel11cf3f42009-10-27 22:16:29 +00003199GetElementPtrInst *GetElementPtrInst::clone_impl() const {
3200 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003201}
3202
Devang Patel11cf3f42009-10-27 22:16:29 +00003203BinaryOperator *BinaryOperator::clone_impl() const {
3204 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003205}
3206
Devang Patel11cf3f42009-10-27 22:16:29 +00003207FCmpInst* FCmpInst::clone_impl() const {
3208 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003209}
3210
Devang Patel11cf3f42009-10-27 22:16:29 +00003211ICmpInst* ICmpInst::clone_impl() const {
3212 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003213}
3214
Devang Patel11cf3f42009-10-27 22:16:29 +00003215ExtractValueInst *ExtractValueInst::clone_impl() const {
3216 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003217}
3218
Devang Patel11cf3f42009-10-27 22:16:29 +00003219InsertValueInst *InsertValueInst::clone_impl() const {
3220 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003221}
3222
Devang Patel11cf3f42009-10-27 22:16:29 +00003223AllocaInst *AllocaInst::clone_impl() const {
3224 return new AllocaInst(getAllocatedType(),
3225 (Value*)getOperand(0),
3226 getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003227}
3228
Devang Patel11cf3f42009-10-27 22:16:29 +00003229LoadInst *LoadInst::clone_impl() const {
3230 return new LoadInst(getOperand(0),
3231 Twine(), isVolatile(),
3232 getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003233}
3234
Devang Patel11cf3f42009-10-27 22:16:29 +00003235StoreInst *StoreInst::clone_impl() const {
3236 return new StoreInst(getOperand(0), getOperand(1),
3237 isVolatile(), getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003238}
3239
Devang Patel11cf3f42009-10-27 22:16:29 +00003240TruncInst *TruncInst::clone_impl() const {
3241 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003242}
3243
Devang Patel11cf3f42009-10-27 22:16:29 +00003244ZExtInst *ZExtInst::clone_impl() const {
3245 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003246}
3247
Devang Patel11cf3f42009-10-27 22:16:29 +00003248SExtInst *SExtInst::clone_impl() const {
3249 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003250}
3251
Devang Patel11cf3f42009-10-27 22:16:29 +00003252FPTruncInst *FPTruncInst::clone_impl() const {
3253 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003254}
3255
Devang Patel11cf3f42009-10-27 22:16:29 +00003256FPExtInst *FPExtInst::clone_impl() const {
3257 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003258}
3259
Devang Patel11cf3f42009-10-27 22:16:29 +00003260UIToFPInst *UIToFPInst::clone_impl() const {
3261 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003262}
3263
Devang Patel11cf3f42009-10-27 22:16:29 +00003264SIToFPInst *SIToFPInst::clone_impl() const {
3265 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003266}
3267
Devang Patel11cf3f42009-10-27 22:16:29 +00003268FPToUIInst *FPToUIInst::clone_impl() const {
3269 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003270}
3271
Devang Patel11cf3f42009-10-27 22:16:29 +00003272FPToSIInst *FPToSIInst::clone_impl() const {
3273 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003274}
3275
Devang Patel11cf3f42009-10-27 22:16:29 +00003276PtrToIntInst *PtrToIntInst::clone_impl() const {
3277 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003278}
3279
Devang Patel11cf3f42009-10-27 22:16:29 +00003280IntToPtrInst *IntToPtrInst::clone_impl() const {
3281 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003282}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003283
Devang Patel11cf3f42009-10-27 22:16:29 +00003284BitCastInst *BitCastInst::clone_impl() const {
3285 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003286}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003287
Devang Patel11cf3f42009-10-27 22:16:29 +00003288CallInst *CallInst::clone_impl() const {
3289 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003290}
3291
Devang Patel11cf3f42009-10-27 22:16:29 +00003292SelectInst *SelectInst::clone_impl() const {
3293 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003294}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003295
Devang Patel11cf3f42009-10-27 22:16:29 +00003296VAArgInst *VAArgInst::clone_impl() const {
3297 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003298}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003299
Devang Patel11cf3f42009-10-27 22:16:29 +00003300ExtractElementInst *ExtractElementInst::clone_impl() const {
3301 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003302}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003303
Devang Patel11cf3f42009-10-27 22:16:29 +00003304InsertElementInst *InsertElementInst::clone_impl() const {
3305 return InsertElementInst::Create(getOperand(0),
3306 getOperand(1),
3307 getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003308}
3309
Devang Patel11cf3f42009-10-27 22:16:29 +00003310ShuffleVectorInst *ShuffleVectorInst::clone_impl() const {
3311 return new ShuffleVectorInst(getOperand(0),
3312 getOperand(1),
3313 getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003314}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003315
Devang Patel11cf3f42009-10-27 22:16:29 +00003316PHINode *PHINode::clone_impl() const {
3317 return new PHINode(*this);
3318}
3319
3320ReturnInst *ReturnInst::clone_impl() const {
3321 return new(getNumOperands()) ReturnInst(*this);
3322}
3323
3324BranchInst *BranchInst::clone_impl() const {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00003325 unsigned Ops(getNumOperands());
Devang Patel11cf3f42009-10-27 22:16:29 +00003326 return new(Ops, Ops == 1) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003327}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003328
Devang Patel11cf3f42009-10-27 22:16:29 +00003329SwitchInst *SwitchInst::clone_impl() const {
3330 return new SwitchInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003331}
3332
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003333IndirectBrInst *IndirectBrInst::clone_impl() const {
3334 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003335}
3336
3337
Devang Patel11cf3f42009-10-27 22:16:29 +00003338InvokeInst *InvokeInst::clone_impl() const {
3339 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003340}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003341
Devang Patel11cf3f42009-10-27 22:16:29 +00003342UnwindInst *UnwindInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003343 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003344 return new UnwindInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003345}
3346
Devang Patel11cf3f42009-10-27 22:16:29 +00003347UnreachableInst *UnreachableInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003348 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003349 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003350}