blob: 6bee186e5702411c3214f4f06485f67b3aa834bd [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//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source 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
15#include "llvm/BasicBlock.h"
16#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/Function.h"
19#include "llvm/Instructions.h"
Reid Spencerce38beb2007-04-09 18:00:57 +000020#include "llvm/ParameterAttributes.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000021#include "llvm/Support/CallSite.h"
Reid Spencer0286bc12007-02-28 22:00:54 +000022#include "llvm/Support/ConstantRange.h"
Christopher Lamb84485702007-04-22 19:24:39 +000023#include "llvm/Support/MathExtras.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000024using namespace llvm;
25
Chris Lattnerf7b6d312005-05-06 20:26:43 +000026unsigned CallSite::getCallingConv() const {
27 if (CallInst *CI = dyn_cast<CallInst>(I))
28 return CI->getCallingConv();
29 else
30 return cast<InvokeInst>(I)->getCallingConv();
31}
32void CallSite::setCallingConv(unsigned CC) {
33 if (CallInst *CI = dyn_cast<CallInst>(I))
34 CI->setCallingConv(CC);
35 else
36 cast<InvokeInst>(I)->setCallingConv(CC);
37}
38
39
Chris Lattner1c12a882006-06-21 16:53:47 +000040
41
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000042//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000043// TerminatorInst Class
44//===----------------------------------------------------------------------===//
45
Chris Lattner1c12a882006-06-21 16:53:47 +000046// Out of line virtual method, so the vtable, etc has a home.
47TerminatorInst::~TerminatorInst() {
48}
49
50// Out of line virtual method, so the vtable, etc has a home.
51UnaryInstruction::~UnaryInstruction() {
52}
Chris Lattnerafdb3de2005-01-29 00:35:16 +000053
54
55//===----------------------------------------------------------------------===//
56// PHINode Class
57//===----------------------------------------------------------------------===//
58
59PHINode::PHINode(const PHINode &PN)
60 : Instruction(PN.getType(), Instruction::PHI,
61 new Use[PN.getNumOperands()], PN.getNumOperands()),
62 ReservedSpace(PN.getNumOperands()) {
63 Use *OL = OperandList;
64 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
65 OL[i].init(PN.getOperand(i), this);
66 OL[i+1].init(PN.getOperand(i+1), this);
67 }
68}
69
70PHINode::~PHINode() {
71 delete [] OperandList;
72}
73
74// removeIncomingValue - Remove an incoming value. This is useful if a
75// predecessor basic block is deleted.
76Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
77 unsigned NumOps = getNumOperands();
78 Use *OL = OperandList;
79 assert(Idx*2 < NumOps && "BB not in PHI node!");
80 Value *Removed = OL[Idx*2];
81
82 // Move everything after this operand down.
83 //
84 // FIXME: we could just swap with the end of the list, then erase. However,
85 // client might not expect this to happen. The code as it is thrashes the
86 // use/def lists, which is kinda lame.
87 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
88 OL[i-2] = OL[i];
89 OL[i-2+1] = OL[i+1];
90 }
91
92 // Nuke the last value.
93 OL[NumOps-2].set(0);
94 OL[NumOps-2+1].set(0);
95 NumOperands = NumOps-2;
96
97 // If the PHI node is dead, because it has zero entries, nuke it now.
98 if (NumOps == 2 && DeletePHIIfEmpty) {
99 // If anyone is using this PHI, make them use a dummy value instead...
100 replaceAllUsesWith(UndefValue::get(getType()));
101 eraseFromParent();
102 }
103 return Removed;
104}
105
106/// resizeOperands - resize operands - This adjusts the length of the operands
107/// list according to the following behavior:
108/// 1. If NumOps == 0, grow the operand list in response to a push_back style
109/// of operation. This grows the number of ops by 1.5 times.
110/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
111/// 3. If NumOps == NumOperands, trim the reserved space.
112///
113void PHINode::resizeOperands(unsigned NumOps) {
114 if (NumOps == 0) {
115 NumOps = (getNumOperands())*3/2;
116 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
117 } else if (NumOps*2 > NumOperands) {
118 // No resize needed.
119 if (ReservedSpace >= NumOps) return;
120 } else if (NumOps == NumOperands) {
121 if (ReservedSpace == NumOps) return;
122 } else {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000123 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000124 }
125
126 ReservedSpace = NumOps;
127 Use *NewOps = new Use[NumOps];
128 Use *OldOps = OperandList;
129 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
130 NewOps[i].init(OldOps[i], this);
131 OldOps[i].set(0);
132 }
133 delete [] OldOps;
134 OperandList = NewOps;
135}
136
Nate Begemanb3923212005-08-04 23:24:19 +0000137/// hasConstantValue - If the specified PHI node always merges together the same
138/// value, return the value, otherwise return null.
139///
Chris Lattner1d8b2482005-08-05 00:49:06 +0000140Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const {
Nate Begemanb3923212005-08-04 23:24:19 +0000141 // If the PHI node only has one incoming value, eliminate the PHI node...
142 if (getNumIncomingValues() == 1)
Chris Lattner6e709c12005-08-05 15:37:31 +0000143 if (getIncomingValue(0) != this) // not X = phi X
144 return getIncomingValue(0);
145 else
146 return UndefValue::get(getType()); // Self cycle is dead.
147
Nate Begemanb3923212005-08-04 23:24:19 +0000148 // Otherwise if all of the incoming values are the same for the PHI, replace
149 // the PHI node with the incoming value.
150 //
151 Value *InVal = 0;
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000152 bool HasUndefInput = false;
Nate Begemanb3923212005-08-04 23:24:19 +0000153 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000154 if (isa<UndefValue>(getIncomingValue(i)))
155 HasUndefInput = true;
156 else if (getIncomingValue(i) != this) // Not the PHI node itself...
Nate Begemanb3923212005-08-04 23:24:19 +0000157 if (InVal && getIncomingValue(i) != InVal)
158 return 0; // Not the same, bail out.
159 else
160 InVal = getIncomingValue(i);
161
162 // The only case that could cause InVal to be null is if we have a PHI node
163 // that only has entries for itself. In this case, there is no entry into the
164 // loop, so kill the PHI.
165 //
166 if (InVal == 0) InVal = UndefValue::get(getType());
167
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000168 // If we have a PHI node like phi(X, undef, X), where X is defined by some
169 // instruction, we cannot always return X as the result of the PHI node. Only
170 // do this if X is not an instruction (thus it must dominate the PHI block),
171 // or if the client is prepared to deal with this possibility.
172 if (HasUndefInput && !AllowNonDominatingInstruction)
173 if (Instruction *IV = dyn_cast<Instruction>(InVal))
174 // If it's in the entry block, it dominates everything.
Dan Gohmandcb291f2007-03-22 16:38:57 +0000175 if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
Chris Lattner37774af2005-08-05 01:03:27 +0000176 isa<InvokeInst>(IV))
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000177 return 0; // Cannot guarantee that InVal dominates this PHINode.
178
Nate Begemanb3923212005-08-04 23:24:19 +0000179 // All of the incoming values are the same, return the value now.
180 return InVal;
181}
182
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000183
184//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000185// CallInst Implementation
186//===----------------------------------------------------------------------===//
187
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000188CallInst::~CallInst() {
189 delete [] OperandList;
Reid Spencerc6a83842007-04-22 17:28:03 +0000190 if (ParamAttrs)
191 ParamAttrs->dropRef();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000192}
193
Chris Lattner054ba2c2007-02-13 00:58:44 +0000194void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
Reid Spencer019c8862007-04-09 15:01:12 +0000195 ParamAttrs = 0;
Chris Lattner054ba2c2007-02-13 00:58:44 +0000196 NumOperands = NumParams+1;
197 Use *OL = OperandList = new Use[NumParams+1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000198 OL[0].init(Func, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000199
Misha Brukmanb1c93172005-04-21 23:48:37 +0000200 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000201 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000202 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000203
Chris Lattner054ba2c2007-02-13 00:58:44 +0000204 assert((NumParams == FTy->getNumParams() ||
205 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000206 "Calling a function with bad signature!");
Chris Lattner054ba2c2007-02-13 00:58:44 +0000207 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000208 assert((i >= FTy->getNumParams() ||
209 FTy->getParamType(i) == Params[i]->getType()) &&
210 "Calling a function with a bad signature!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000211 OL[i+1].init(Params[i], this);
Chris Lattner667a0562006-05-03 00:48:22 +0000212 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000213}
214
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000215void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
Reid Spencer019c8862007-04-09 15:01:12 +0000216 ParamAttrs = 0;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000217 NumOperands = 3;
218 Use *OL = OperandList = new Use[3];
219 OL[0].init(Func, this);
220 OL[1].init(Actual1, this);
221 OL[2].init(Actual2, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000222
223 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000224 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000225 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000226
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000227 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000228 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000229 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000230 assert((0 >= FTy->getNumParams() ||
231 FTy->getParamType(0) == Actual1->getType()) &&
232 "Calling a function with a bad signature!");
233 assert((1 >= FTy->getNumParams() ||
234 FTy->getParamType(1) == Actual2->getType()) &&
235 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000236}
237
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000238void CallInst::init(Value *Func, Value *Actual) {
Reid Spencer019c8862007-04-09 15:01:12 +0000239 ParamAttrs = 0;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000240 NumOperands = 2;
241 Use *OL = OperandList = new Use[2];
242 OL[0].init(Func, this);
243 OL[1].init(Actual, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000244
245 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000246 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000247 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000248
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000249 assert((FTy->getNumParams() == 1 ||
250 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000251 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000252 assert((0 == FTy->getNumParams() ||
253 FTy->getParamType(0) == Actual->getType()) &&
254 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000255}
256
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000257void CallInst::init(Value *Func) {
Reid Spencer019c8862007-04-09 15:01:12 +0000258 ParamAttrs = 0;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000259 NumOperands = 1;
260 Use *OL = OperandList = new Use[1];
261 OL[0].init(Func, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000262
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000263 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000264 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000265 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000266
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000267 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000268}
269
David Greene17a5dfe2007-08-01 03:43:44 +0000270#if 0
271// Leave for llvm-gcc
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000272CallInst::CallInst(Value *Func, Value* const *Args, unsigned NumArgs,
Misha Brukmanb1c93172005-04-21 23:48:37 +0000273 const std::string &Name, BasicBlock *InsertAtEnd)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000274 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
David Greene17a5dfe2007-08-01 03:43:44 +0000275 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000276 Instruction::Call, 0, 0, InsertAtEnd) {
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000277 init(Func, Args, NumArgs);
Chris Lattner2195fc42007-02-24 00:55:48 +0000278 setName(Name);
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000279}
280CallInst::CallInst(Value *Func, Value* const *Args, unsigned NumArgs,
281 const std::string &Name, Instruction *InsertBefore)
David Greene17a5dfe2007-08-01 03:43:44 +0000282 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
283 ->getElementType())->getReturnType(),
284 Instruction::Call, 0, 0, InsertBefore) {
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000285 init(Func, Args, NumArgs);
Chris Lattner2195fc42007-02-24 00:55:48 +0000286 setName(Name);
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000287}
288
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000289CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2,
290 const std::string &Name, Instruction *InsertBefore)
291 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
292 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000293 Instruction::Call, 0, 0, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000294 init(Func, Actual1, Actual2);
Chris Lattner2195fc42007-02-24 00:55:48 +0000295 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000296}
297
298CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2,
299 const std::string &Name, BasicBlock *InsertAtEnd)
300 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
301 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000302 Instruction::Call, 0, 0, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000303 init(Func, Actual1, Actual2);
Chris Lattner2195fc42007-02-24 00:55:48 +0000304 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000305}
David Greene17a5dfe2007-08-01 03:43:44 +0000306#endif
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000307CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +0000308 Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000309 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
310 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000311 Instruction::Call, 0, 0, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000312 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000313 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000314}
315
316CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
317 BasicBlock *InsertAtEnd)
318 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
319 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000320 Instruction::Call, 0, 0, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000321 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000322 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000323}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000324CallInst::CallInst(Value *Func, const std::string &Name,
325 Instruction *InsertBefore)
326 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
327 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000328 Instruction::Call, 0, 0, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000329 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000330 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000331}
332
333CallInst::CallInst(Value *Func, const std::string &Name,
334 BasicBlock *InsertAtEnd)
335 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
336 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000337 Instruction::Call, 0, 0, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000338 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000339 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000340}
341
Misha Brukmanb1c93172005-04-21 23:48:37 +0000342CallInst::CallInst(const CallInst &CI)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000343 : Instruction(CI.getType(), Instruction::Call, new Use[CI.getNumOperands()],
344 CI.getNumOperands()) {
Reid Spencerce38beb2007-04-09 18:00:57 +0000345 ParamAttrs = 0;
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000346 SubclassData = CI.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000347 Use *OL = OperandList;
348 Use *InOL = CI.OperandList;
349 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
350 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000351}
352
Reid Spencerc6a83842007-04-22 17:28:03 +0000353void CallInst::setParamAttrs(ParamAttrsList *newAttrs) {
354 if (ParamAttrs)
355 ParamAttrs->dropRef();
356
357 if (newAttrs)
358 newAttrs->addRef();
359
360 ParamAttrs = newAttrs;
361}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000362
363//===----------------------------------------------------------------------===//
364// InvokeInst Implementation
365//===----------------------------------------------------------------------===//
366
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000367InvokeInst::~InvokeInst() {
368 delete [] OperandList;
Reid Spencerc6a83842007-04-22 17:28:03 +0000369 if (ParamAttrs)
370 ParamAttrs->dropRef();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000371}
372
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000373void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000374 Value* const *Args, unsigned NumArgs) {
Reid Spencerce38beb2007-04-09 18:00:57 +0000375 ParamAttrs = 0;
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000376 NumOperands = 3+NumArgs;
377 Use *OL = OperandList = new Use[3+NumArgs];
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000378 OL[0].init(Fn, this);
379 OL[1].init(IfNormal, this);
380 OL[2].init(IfException, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000381 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000382 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000383 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000384
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000385 assert((NumArgs == FTy->getNumParams()) ||
386 (FTy->isVarArg() && NumArgs > FTy->getNumParams()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000387 "Calling a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000388
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000389 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000390 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000391 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000392 "Invoking a function with a bad signature!");
393
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000394 OL[i+3].init(Args[i], this);
Chris Lattner667a0562006-05-03 00:48:22 +0000395 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000396}
397
Misha Brukmanb1c93172005-04-21 23:48:37 +0000398InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000399 : TerminatorInst(II.getType(), Instruction::Invoke,
400 new Use[II.getNumOperands()], II.getNumOperands()) {
Reid Spencerce38beb2007-04-09 18:00:57 +0000401 ParamAttrs = 0;
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000402 SubclassData = II.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000403 Use *OL = OperandList, *InOL = II.OperandList;
404 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
405 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000406}
407
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000408BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
409 return getSuccessor(idx);
410}
411unsigned InvokeInst::getNumSuccessorsV() const {
412 return getNumSuccessors();
413}
414void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
415 return setSuccessor(idx, B);
416}
417
Reid Spencerc6a83842007-04-22 17:28:03 +0000418void InvokeInst::setParamAttrs(ParamAttrsList *newAttrs) {
419 if (ParamAttrs)
420 ParamAttrs->dropRef();
421
422 if (newAttrs)
423 newAttrs->addRef();
424
425 ParamAttrs = newAttrs;
426}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000427
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000428//===----------------------------------------------------------------------===//
429// ReturnInst Implementation
430//===----------------------------------------------------------------------===//
431
Chris Lattner2195fc42007-02-24 00:55:48 +0000432ReturnInst::ReturnInst(const ReturnInst &RI)
433 : TerminatorInst(Type::VoidTy, Instruction::Ret,
434 &RetVal, RI.getNumOperands()) {
435 if (RI.getNumOperands())
436 RetVal.init(RI.RetVal, this);
437}
438
439ReturnInst::ReturnInst(Value *retVal, Instruction *InsertBefore)
440 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertBefore) {
441 init(retVal);
442}
443ReturnInst::ReturnInst(Value *retVal, BasicBlock *InsertAtEnd)
444 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertAtEnd) {
445 init(retVal);
446}
447ReturnInst::ReturnInst(BasicBlock *InsertAtEnd)
448 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertAtEnd) {
449}
450
451
452
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000453void ReturnInst::init(Value *retVal) {
454 if (retVal && retVal->getType() != Type::VoidTy) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000455 assert(!isa<BasicBlock>(retVal) &&
Alkis Evlogimenos531e9012004-11-17 21:02:25 +0000456 "Cannot return basic block. Probably using the incorrect ctor");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000457 NumOperands = 1;
458 RetVal.init(retVal, this);
Alkis Evlogimenos531e9012004-11-17 21:02:25 +0000459 }
460}
461
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000462unsigned ReturnInst::getNumSuccessorsV() const {
463 return getNumSuccessors();
464}
465
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000466// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
467// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000468void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000469 assert(0 && "ReturnInst has no successors!");
470}
471
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000472BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
473 assert(0 && "ReturnInst has no successors!");
474 abort();
475 return 0;
476}
477
478
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000479//===----------------------------------------------------------------------===//
480// UnwindInst Implementation
481//===----------------------------------------------------------------------===//
482
Chris Lattner2195fc42007-02-24 00:55:48 +0000483UnwindInst::UnwindInst(Instruction *InsertBefore)
484 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertBefore) {
485}
486UnwindInst::UnwindInst(BasicBlock *InsertAtEnd)
487 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertAtEnd) {
488}
489
490
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000491unsigned UnwindInst::getNumSuccessorsV() const {
492 return getNumSuccessors();
493}
494
495void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000496 assert(0 && "UnwindInst has no successors!");
497}
498
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000499BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
500 assert(0 && "UnwindInst has no successors!");
501 abort();
502 return 0;
503}
504
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000505//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000506// UnreachableInst Implementation
507//===----------------------------------------------------------------------===//
508
Chris Lattner2195fc42007-02-24 00:55:48 +0000509UnreachableInst::UnreachableInst(Instruction *InsertBefore)
510 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertBefore) {
511}
512UnreachableInst::UnreachableInst(BasicBlock *InsertAtEnd)
513 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertAtEnd) {
514}
515
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000516unsigned UnreachableInst::getNumSuccessorsV() const {
517 return getNumSuccessors();
518}
519
520void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
521 assert(0 && "UnwindInst has no successors!");
522}
523
524BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
525 assert(0 && "UnwindInst has no successors!");
526 abort();
527 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000528}
529
530//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000531// BranchInst Implementation
532//===----------------------------------------------------------------------===//
533
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000534void BranchInst::AssertOK() {
535 if (isConditional())
Reid Spencer542964f2007-01-11 18:21:29 +0000536 assert(getCondition()->getType() == Type::Int1Ty &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000537 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000538}
539
Chris Lattner2195fc42007-02-24 00:55:48 +0000540BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
541 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 1, InsertBefore) {
542 assert(IfTrue != 0 && "Branch destination may not be null!");
543 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
544}
545BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
546 Instruction *InsertBefore)
547: TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 3, InsertBefore) {
548 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
549 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
550 Ops[2].init(Cond, this);
551#ifndef NDEBUG
552 AssertOK();
553#endif
554}
555
556BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
557 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 1, InsertAtEnd) {
558 assert(IfTrue != 0 && "Branch destination may not be null!");
559 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
560}
561
562BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
563 BasicBlock *InsertAtEnd)
564 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 3, InsertAtEnd) {
565 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
566 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
567 Ops[2].init(Cond, this);
568#ifndef NDEBUG
569 AssertOK();
570#endif
571}
572
573
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000574BranchInst::BranchInst(const BranchInst &BI) :
Chris Lattner2195fc42007-02-24 00:55:48 +0000575 TerminatorInst(Type::VoidTy, Instruction::Br, Ops, BI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000576 OperandList[0].init(BI.getOperand(0), this);
577 if (BI.getNumOperands() != 1) {
578 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
579 OperandList[1].init(BI.getOperand(1), this);
580 OperandList[2].init(BI.getOperand(2), this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000581 }
582}
583
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000584BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
585 return getSuccessor(idx);
586}
587unsigned BranchInst::getNumSuccessorsV() const {
588 return getNumSuccessors();
589}
590void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
591 setSuccessor(idx, B);
592}
593
594
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000595//===----------------------------------------------------------------------===//
596// AllocationInst Implementation
597//===----------------------------------------------------------------------===//
598
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000599static Value *getAISize(Value *Amt) {
600 if (!Amt)
Reid Spencer8d9336d2006-12-31 05:26:44 +0000601 Amt = ConstantInt::get(Type::Int32Ty, 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000602 else {
603 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000604 "Passed basic block into allocation size parameter! Use other ctor");
Reid Spencer8d9336d2006-12-31 05:26:44 +0000605 assert(Amt->getType() == Type::Int32Ty &&
Reid Spencer7e16e232007-01-26 06:30:34 +0000606 "Malloc/Allocation array size is not a 32-bit integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000607 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000608 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000609}
610
Misha Brukmanb1c93172005-04-21 23:48:37 +0000611AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000612 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000613 Instruction *InsertBefore)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000614 : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize),
Chris Lattner2195fc42007-02-24 00:55:48 +0000615 InsertBefore), Alignment(Align) {
Chris Lattner79b8c792005-11-05 21:57:54 +0000616 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000617 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000618 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000619}
620
Misha Brukmanb1c93172005-04-21 23:48:37 +0000621AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000622 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000623 BasicBlock *InsertAtEnd)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000624 : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize),
Chris Lattner2195fc42007-02-24 00:55:48 +0000625 InsertAtEnd), Alignment(Align) {
Chris Lattner79b8c792005-11-05 21:57:54 +0000626 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000627 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000628 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000629}
630
Chris Lattner1c12a882006-06-21 16:53:47 +0000631// Out of line virtual method, so the vtable, etc has a home.
632AllocationInst::~AllocationInst() {
633}
634
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000635bool AllocationInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000636 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
637 return CI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000638 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000639}
640
641const Type *AllocationInst::getAllocatedType() const {
642 return getType()->getElementType();
643}
644
645AllocaInst::AllocaInst(const AllocaInst &AI)
646 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000647 Instruction::Alloca, AI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000648}
649
650MallocInst::MallocInst(const MallocInst &MI)
651 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000652 Instruction::Malloc, MI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000653}
654
655//===----------------------------------------------------------------------===//
656// FreeInst Implementation
657//===----------------------------------------------------------------------===//
658
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000659void FreeInst::AssertOK() {
660 assert(isa<PointerType>(getOperand(0)->getType()) &&
661 "Can not free something of nonpointer type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000662}
663
664FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000665 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000666 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000667}
668
669FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000670 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000671 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000672}
673
674
675//===----------------------------------------------------------------------===//
676// LoadInst Implementation
677//===----------------------------------------------------------------------===//
678
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000679void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000680 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000681 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000682}
683
684LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000685 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000686 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000687 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000688 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000689 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000690 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000691}
692
693LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000694 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000695 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000696 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000697 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000698 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000699 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000700}
701
702LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
703 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000704 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000705 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000706 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000707 setAlignment(0);
708 AssertOK();
709 setName(Name);
710}
711
712LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
713 unsigned Align, Instruction *InsertBef)
714 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
715 Load, Ptr, InsertBef) {
716 setVolatile(isVolatile);
717 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000718 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000719 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000720}
721
Dan Gohman68659282007-07-18 20:51:11 +0000722LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
723 unsigned Align, BasicBlock *InsertAE)
724 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
725 Load, Ptr, InsertAE) {
726 setVolatile(isVolatile);
727 setAlignment(Align);
728 AssertOK();
729 setName(Name);
730}
731
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000732LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
733 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000734 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000735 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000736 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000737 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000738 AssertOK();
739 setName(Name);
740}
741
742
743
744LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +0000745 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
746 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000747 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000748 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000749 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000750 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000751}
752
753LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000754 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
755 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000756 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000757 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000758 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000759 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000760}
761
762LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
763 Instruction *InsertBef)
764: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000765 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000766 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000767 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000768 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000769 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000770}
771
772LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
773 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000774 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
775 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000776 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000777 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000778 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000779 if (Name && Name[0]) setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000780}
781
Christopher Lamb84485702007-04-22 19:24:39 +0000782void LoadInst::setAlignment(unsigned Align) {
783 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
784 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
785}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000786
787//===----------------------------------------------------------------------===//
788// StoreInst Implementation
789//===----------------------------------------------------------------------===//
790
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000791void StoreInst::AssertOK() {
792 assert(isa<PointerType>(getOperand(1)->getType()) &&
793 "Ptr must have pointer type!");
794 assert(getOperand(0)->getType() ==
795 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000796 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000797}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000798
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000799
800StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000801 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000802 Ops[0].init(val, this);
803 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000804 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000805 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000806 AssertOK();
807}
808
809StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000810 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000811 Ops[0].init(val, this);
812 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000813 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000814 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000815 AssertOK();
816}
817
Misha Brukmanb1c93172005-04-21 23:48:37 +0000818StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000819 Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000820 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000821 Ops[0].init(val, this);
822 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000823 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000824 setAlignment(0);
825 AssertOK();
826}
827
828StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
829 unsigned Align, Instruction *InsertBefore)
830 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
831 Ops[0].init(val, this);
832 Ops[1].init(addr, this);
833 setVolatile(isVolatile);
834 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000835 AssertOK();
836}
837
Misha Brukmanb1c93172005-04-21 23:48:37 +0000838StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000839 unsigned Align, BasicBlock *InsertAtEnd)
840 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
841 Ops[0].init(val, this);
842 Ops[1].init(addr, this);
843 setVolatile(isVolatile);
844 setAlignment(Align);
845 AssertOK();
846}
847
848StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000849 BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000850 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000851 Ops[0].init(val, this);
852 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000853 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000854 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000855 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000856}
857
Christopher Lamb84485702007-04-22 19:24:39 +0000858void StoreInst::setAlignment(unsigned Align) {
859 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
860 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
861}
862
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000863//===----------------------------------------------------------------------===//
864// GetElementPtrInst Implementation
865//===----------------------------------------------------------------------===//
866
Chris Lattner79807c3d2007-01-31 19:47:18 +0000867void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx) {
868 NumOperands = 1+NumIdx;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000869 Use *OL = OperandList = new Use[NumOperands];
870 OL[0].init(Ptr, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000871
Chris Lattner79807c3d2007-01-31 19:47:18 +0000872 for (unsigned i = 0; i != NumIdx; ++i)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000873 OL[i+1].init(Idx[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000874}
875
Chris Lattner82981202005-05-03 05:43:30 +0000876void GetElementPtrInst::init(Value *Ptr, Value *Idx) {
877 NumOperands = 2;
878 Use *OL = OperandList = new Use[2];
879 OL[0].init(Ptr, this);
880 OL[1].init(Idx, this);
881}
882
Chris Lattner82981202005-05-03 05:43:30 +0000883GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
884 const std::string &Name, Instruction *InBe)
Chris Lattner2195fc42007-02-24 00:55:48 +0000885 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx))),
886 GetElementPtr, 0, 0, InBe) {
Chris Lattner82981202005-05-03 05:43:30 +0000887 init(Ptr, Idx);
Chris Lattner2195fc42007-02-24 00:55:48 +0000888 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +0000889}
890
891GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
892 const std::string &Name, BasicBlock *IAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000893 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx))),
894 GetElementPtr, 0, 0, IAE) {
Chris Lattner82981202005-05-03 05:43:30 +0000895 init(Ptr, Idx);
Chris Lattner2195fc42007-02-24 00:55:48 +0000896 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +0000897}
898
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000899GetElementPtrInst::~GetElementPtrInst() {
900 delete[] OperandList;
901}
902
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000903// getIndexedType - Returns the type of the element that would be loaded with
904// a load instruction with the specified parameters.
905//
Misha Brukmanb1c93172005-04-21 23:48:37 +0000906// A null type is returned if the indices are invalid for the specified
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000907// pointer type.
908//
Misha Brukmanb1c93172005-04-21 23:48:37 +0000909const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Chris Lattner302116a2007-01-31 04:40:28 +0000910 Value* const *Idxs,
911 unsigned NumIdx,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000912 bool AllowCompositeLeaf) {
913 if (!isa<PointerType>(Ptr)) return 0; // Type isn't a pointer type!
914
915 // Handle the special case of the empty set index set...
Chris Lattner302116a2007-01-31 04:40:28 +0000916 if (NumIdx == 0)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000917 if (AllowCompositeLeaf ||
918 cast<PointerType>(Ptr)->getElementType()->isFirstClassType())
919 return cast<PointerType>(Ptr)->getElementType();
920 else
921 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000922
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000923 unsigned CurIdx = 0;
924 while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
Chris Lattner302116a2007-01-31 04:40:28 +0000925 if (NumIdx == CurIdx) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000926 if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr;
927 return 0; // Can't load a whole structure or array!?!?
928 }
929
Chris Lattner302116a2007-01-31 04:40:28 +0000930 Value *Index = Idxs[CurIdx++];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000931 if (isa<PointerType>(CT) && CurIdx != 1)
932 return 0; // Can only index into pointer types at the first index!
933 if (!CT->indexValid(Index)) return 0;
934 Ptr = CT->getTypeAtIndex(Index);
935
936 // If the new type forwards to another type, then it is in the middle
937 // of being refined to another type (and hence, may have dropped all
938 // references to what it was using before). So, use the new forwarded
939 // type.
940 if (const Type * Ty = Ptr->getForwardedType()) {
941 Ptr = Ty;
942 }
943 }
Chris Lattner302116a2007-01-31 04:40:28 +0000944 return CurIdx == NumIdx ? Ptr : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000945}
946
Chris Lattner82981202005-05-03 05:43:30 +0000947const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
948 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
949 if (!PTy) return 0; // Type isn't a pointer type!
950
951 // Check the pointer index.
952 if (!PTy->indexValid(Idx)) return 0;
953
Chris Lattnerc2233332005-05-03 16:44:45 +0000954 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +0000955}
956
Chris Lattner45f15572007-04-14 00:12:57 +0000957
958/// hasAllZeroIndices - Return true if all of the indices of this GEP are
959/// zeros. If so, the result pointer and the first operand have the same
960/// value, just potentially different types.
961bool GetElementPtrInst::hasAllZeroIndices() const {
962 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
963 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
964 if (!CI->isZero()) return false;
965 } else {
966 return false;
967 }
968 }
969 return true;
970}
971
Chris Lattner27058292007-04-27 20:35:56 +0000972/// hasAllConstantIndices - Return true if all of the indices of this GEP are
973/// constant integers. If so, the result pointer and the first operand have
974/// a constant offset between them.
975bool GetElementPtrInst::hasAllConstantIndices() const {
976 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
977 if (!isa<ConstantInt>(getOperand(i)))
978 return false;
979 }
980 return true;
981}
982
Chris Lattner45f15572007-04-14 00:12:57 +0000983
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000984//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +0000985// ExtractElementInst Implementation
986//===----------------------------------------------------------------------===//
987
988ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000989 const std::string &Name,
990 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +0000991 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000992 ExtractElement, Ops, 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +0000993 assert(isValidOperands(Val, Index) &&
994 "Invalid extractelement instruction operands!");
Robert Bocchino23004482006-01-10 19:05:34 +0000995 Ops[0].init(Val, this);
996 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +0000997 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +0000998}
999
Chris Lattner65511ff2006-10-05 06:24:58 +00001000ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1001 const std::string &Name,
1002 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001003 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001004 ExtractElement, Ops, 2, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001005 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001006 assert(isValidOperands(Val, Index) &&
1007 "Invalid extractelement instruction operands!");
1008 Ops[0].init(Val, this);
1009 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001010 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001011}
1012
1013
Robert Bocchino23004482006-01-10 19:05:34 +00001014ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001015 const std::string &Name,
1016 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001017 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001018 ExtractElement, Ops, 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001019 assert(isValidOperands(Val, Index) &&
1020 "Invalid extractelement instruction operands!");
1021
Robert Bocchino23004482006-01-10 19:05:34 +00001022 Ops[0].init(Val, this);
1023 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001024 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001025}
1026
Chris Lattner65511ff2006-10-05 06:24:58 +00001027ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1028 const std::string &Name,
1029 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001030 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001031 ExtractElement, Ops, 2, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001032 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001033 assert(isValidOperands(Val, Index) &&
1034 "Invalid extractelement instruction operands!");
1035
1036 Ops[0].init(Val, this);
1037 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001038 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001039}
1040
1041
Chris Lattner54865b32006-04-08 04:05:48 +00001042bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001043 if (!isa<VectorType>(Val->getType()) || Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001044 return false;
1045 return true;
1046}
1047
1048
Robert Bocchino23004482006-01-10 19:05:34 +00001049//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001050// InsertElementInst Implementation
1051//===----------------------------------------------------------------------===//
1052
Chris Lattner0875d942006-04-14 22:20:32 +00001053InsertElementInst::InsertElementInst(const InsertElementInst &IE)
1054 : Instruction(IE.getType(), InsertElement, Ops, 3) {
1055 Ops[0].init(IE.Ops[0], this);
1056 Ops[1].init(IE.Ops[1], this);
1057 Ops[2].init(IE.Ops[2], this);
1058}
Chris Lattner54865b32006-04-08 04:05:48 +00001059InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001060 const std::string &Name,
1061 Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +00001062 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001063 assert(isValidOperands(Vec, Elt, Index) &&
1064 "Invalid insertelement instruction operands!");
1065 Ops[0].init(Vec, this);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001066 Ops[1].init(Elt, this);
1067 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001068 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001069}
1070
Chris Lattner65511ff2006-10-05 06:24:58 +00001071InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1072 const std::string &Name,
1073 Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +00001074 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001075 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001076 assert(isValidOperands(Vec, Elt, Index) &&
1077 "Invalid insertelement instruction operands!");
1078 Ops[0].init(Vec, this);
1079 Ops[1].init(Elt, this);
1080 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001081 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001082}
1083
1084
Chris Lattner54865b32006-04-08 04:05:48 +00001085InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001086 const std::string &Name,
1087 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +00001088 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001089 assert(isValidOperands(Vec, Elt, Index) &&
1090 "Invalid insertelement instruction operands!");
1091
1092 Ops[0].init(Vec, this);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001093 Ops[1].init(Elt, this);
1094 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001095 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001096}
1097
Chris Lattner65511ff2006-10-05 06:24:58 +00001098InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1099 const std::string &Name,
1100 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +00001101: Instruction(Vec->getType(), InsertElement, Ops, 3, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001102 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001103 assert(isValidOperands(Vec, Elt, Index) &&
1104 "Invalid insertelement instruction operands!");
1105
1106 Ops[0].init(Vec, this);
1107 Ops[1].init(Elt, this);
1108 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001109 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001110}
1111
Chris Lattner54865b32006-04-08 04:05:48 +00001112bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1113 const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001114 if (!isa<VectorType>(Vec->getType()))
Reid Spencer09575ba2007-02-15 03:39:18 +00001115 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001116
Reid Spencerd84d35b2007-02-15 02:26:10 +00001117 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001118 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001119
Reid Spencer8d9336d2006-12-31 05:26:44 +00001120 if (Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001121 return false; // Third operand of insertelement must be uint.
1122 return true;
1123}
1124
1125
Robert Bocchinoca27f032006-01-17 20:07:22 +00001126//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001127// ShuffleVectorInst Implementation
1128//===----------------------------------------------------------------------===//
1129
Chris Lattner0875d942006-04-14 22:20:32 +00001130ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV)
1131 : Instruction(SV.getType(), ShuffleVector, Ops, 3) {
1132 Ops[0].init(SV.Ops[0], this);
1133 Ops[1].init(SV.Ops[1], this);
1134 Ops[2].init(SV.Ops[2], this);
1135}
1136
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001137ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1138 const std::string &Name,
1139 Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +00001140 : Instruction(V1->getType(), ShuffleVector, Ops, 3, InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001141 assert(isValidOperands(V1, V2, Mask) &&
1142 "Invalid shuffle vector instruction operands!");
1143 Ops[0].init(V1, this);
1144 Ops[1].init(V2, this);
1145 Ops[2].init(Mask, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001146 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001147}
1148
1149ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1150 const std::string &Name,
1151 BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +00001152 : Instruction(V1->getType(), ShuffleVector, Ops, 3, InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001153 assert(isValidOperands(V1, V2, Mask) &&
1154 "Invalid shuffle vector instruction operands!");
1155
1156 Ops[0].init(V1, this);
1157 Ops[1].init(V2, this);
1158 Ops[2].init(Mask, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001159 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001160}
1161
1162bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
1163 const Value *Mask) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001164 if (!isa<VectorType>(V1->getType())) return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001165 if (V1->getType() != V2->getType()) return false;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001166 if (!isa<VectorType>(Mask->getType()) ||
1167 cast<VectorType>(Mask->getType())->getElementType() != Type::Int32Ty ||
1168 cast<VectorType>(Mask->getType())->getNumElements() !=
1169 cast<VectorType>(V1->getType())->getNumElements())
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001170 return false;
1171 return true;
1172}
1173
1174
1175//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001176// BinaryOperator Class
1177//===----------------------------------------------------------------------===//
1178
Chris Lattner2195fc42007-02-24 00:55:48 +00001179BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1180 const Type *Ty, const std::string &Name,
1181 Instruction *InsertBefore)
1182 : Instruction(Ty, iType, Ops, 2, InsertBefore) {
1183 Ops[0].init(S1, this);
1184 Ops[1].init(S2, this);
1185 init(iType);
1186 setName(Name);
1187}
1188
1189BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1190 const Type *Ty, const std::string &Name,
1191 BasicBlock *InsertAtEnd)
1192 : Instruction(Ty, iType, Ops, 2, InsertAtEnd) {
1193 Ops[0].init(S1, this);
1194 Ops[1].init(S2, this);
1195 init(iType);
1196 setName(Name);
1197}
1198
1199
1200void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001201 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001202 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001203 assert(LHS->getType() == RHS->getType() &&
1204 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001205#ifndef NDEBUG
1206 switch (iType) {
1207 case Add: case Sub:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001208 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001209 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001210 "Arithmetic operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001211 assert((getType()->isInteger() || getType()->isFloatingPoint() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001212 isa<VectorType>(getType())) &&
Brian Gaeke02209042004-08-20 06:00:58 +00001213 "Tried to create an arithmetic operation on a non-arithmetic type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001214 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001215 case UDiv:
1216 case SDiv:
1217 assert(getType() == LHS->getType() &&
1218 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001219 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1220 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001221 "Incorrect operand type (not integer) for S/UDIV");
1222 break;
1223 case FDiv:
1224 assert(getType() == LHS->getType() &&
1225 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001226 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1227 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001228 && "Incorrect operand type (not floating point) for FDIV");
1229 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001230 case URem:
1231 case SRem:
1232 assert(getType() == LHS->getType() &&
1233 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001234 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1235 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001236 "Incorrect operand type (not integer) for S/UREM");
1237 break;
1238 case FRem:
1239 assert(getType() == LHS->getType() &&
1240 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001241 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1242 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7eb55b32006-11-02 01:53:59 +00001243 && "Incorrect operand type (not floating point) for FREM");
1244 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001245 case Shl:
1246 case LShr:
1247 case AShr:
1248 assert(getType() == LHS->getType() &&
1249 "Shift operation should return same type as operands!");
1250 assert(getType()->isInteger() &&
1251 "Shift operation requires integer operands");
1252 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001253 case And: case Or:
1254 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001255 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001256 "Logical operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001257 assert((getType()->isInteger() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001258 (isa<VectorType>(getType()) &&
1259 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001260 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001261 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001262 default:
1263 break;
1264 }
1265#endif
1266}
1267
1268BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001269 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001270 Instruction *InsertBefore) {
1271 assert(S1->getType() == S2->getType() &&
1272 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001273 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001274}
1275
1276BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001277 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001278 BasicBlock *InsertAtEnd) {
1279 BinaryOperator *Res = create(Op, S1, S2, Name);
1280 InsertAtEnd->getInstList().push_back(Res);
1281 return Res;
1282}
1283
1284BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1285 Instruction *InsertBefore) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001286 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1287 return new BinaryOperator(Instruction::Sub,
1288 zero, Op,
1289 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001290}
1291
1292BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1293 BasicBlock *InsertAtEnd) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001294 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1295 return new BinaryOperator(Instruction::Sub,
1296 zero, Op,
1297 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001298}
1299
1300BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1301 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001302 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001303 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001304 C = ConstantInt::getAllOnesValue(PTy->getElementType());
Reid Spencerd84d35b2007-02-15 02:26:10 +00001305 C = ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001306 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001307 C = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001308 }
1309
1310 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001311 Op->getType(), Name, InsertBefore);
1312}
1313
1314BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1315 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001316 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001317 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001318 // Create a vector of all ones values.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001319 Constant *Elt = ConstantInt::getAllOnesValue(PTy->getElementType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001320 AllOnes =
Reid Spencerd84d35b2007-02-15 02:26:10 +00001321 ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001322 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001323 AllOnes = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001324 }
1325
1326 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001327 Op->getType(), Name, InsertAtEnd);
1328}
1329
1330
1331// isConstantAllOnes - Helper function for several functions below
1332static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001333 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1334 return CI->isAllOnesValue();
1335 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1336 return CV->isAllOnesValue();
1337 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001338}
1339
1340bool BinaryOperator::isNeg(const Value *V) {
1341 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1342 if (Bop->getOpcode() == Instruction::Sub)
Reid Spencer2eadb532007-01-21 00:29:26 +00001343 return Bop->getOperand(0) ==
1344 ConstantExpr::getZeroValueForNegationExpr(Bop->getType());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001345 return false;
1346}
1347
1348bool BinaryOperator::isNot(const Value *V) {
1349 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1350 return (Bop->getOpcode() == Instruction::Xor &&
1351 (isConstantAllOnes(Bop->getOperand(1)) ||
1352 isConstantAllOnes(Bop->getOperand(0))));
1353 return false;
1354}
1355
Chris Lattner2c7d1772005-04-24 07:28:37 +00001356Value *BinaryOperator::getNegArgument(Value *BinOp) {
1357 assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!");
1358 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001359}
1360
Chris Lattner2c7d1772005-04-24 07:28:37 +00001361const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1362 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001363}
1364
Chris Lattner2c7d1772005-04-24 07:28:37 +00001365Value *BinaryOperator::getNotArgument(Value *BinOp) {
1366 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1367 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1368 Value *Op0 = BO->getOperand(0);
1369 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001370 if (isConstantAllOnes(Op0)) return Op1;
1371
1372 assert(isConstantAllOnes(Op1));
1373 return Op0;
1374}
1375
Chris Lattner2c7d1772005-04-24 07:28:37 +00001376const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1377 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001378}
1379
1380
1381// swapOperands - Exchange the two operands to this instruction. This
1382// instruction is safe to use on any binary instruction and does not
1383// modify the semantics of the instruction. If the instruction is
1384// order dependent (SetLT f.e.) the opcode is changed.
1385//
1386bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001387 if (!isCommutative())
1388 return true; // Can't commute operands
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001389 std::swap(Ops[0], Ops[1]);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001390 return false;
1391}
1392
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001393//===----------------------------------------------------------------------===//
1394// CastInst Class
1395//===----------------------------------------------------------------------===//
1396
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001397// Just determine if this cast only deals with integral->integral conversion.
1398bool CastInst::isIntegerCast() const {
1399 switch (getOpcode()) {
1400 default: return false;
1401 case Instruction::ZExt:
1402 case Instruction::SExt:
1403 case Instruction::Trunc:
1404 return true;
1405 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001406 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001407 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001408}
1409
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001410bool CastInst::isLosslessCast() const {
1411 // Only BitCast can be lossless, exit fast if we're not BitCast
1412 if (getOpcode() != Instruction::BitCast)
1413 return false;
1414
1415 // Identity cast is always lossless
1416 const Type* SrcTy = getOperand(0)->getType();
1417 const Type* DstTy = getType();
1418 if (SrcTy == DstTy)
1419 return true;
1420
Reid Spencer8d9336d2006-12-31 05:26:44 +00001421 // Pointer to pointer is always lossless.
1422 if (isa<PointerType>(SrcTy))
1423 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001424 return false; // Other types have no identity values
1425}
1426
1427/// This function determines if the CastInst does not require any bits to be
1428/// changed in order to effect the cast. Essentially, it identifies cases where
1429/// no code gen is necessary for the cast, hence the name no-op cast. For
1430/// example, the following are all no-op casts:
1431/// # bitcast uint %X, int
1432/// # bitcast uint* %x, sbyte*
Dan Gohmanfead7972007-05-11 21:43:24 +00001433/// # bitcast vector< 2 x int > %x, vector< 4 x short>
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001434/// # ptrtoint uint* %x, uint ; on 32-bit plaforms only
1435/// @brief Determine if a cast is a no-op.
1436bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1437 switch (getOpcode()) {
1438 default:
1439 assert(!"Invalid CastOp");
1440 case Instruction::Trunc:
1441 case Instruction::ZExt:
1442 case Instruction::SExt:
1443 case Instruction::FPTrunc:
1444 case Instruction::FPExt:
1445 case Instruction::UIToFP:
1446 case Instruction::SIToFP:
1447 case Instruction::FPToUI:
1448 case Instruction::FPToSI:
1449 return false; // These always modify bits
1450 case Instruction::BitCast:
1451 return true; // BitCast never modifies bits.
1452 case Instruction::PtrToInt:
1453 return IntPtrTy->getPrimitiveSizeInBits() ==
1454 getType()->getPrimitiveSizeInBits();
1455 case Instruction::IntToPtr:
1456 return IntPtrTy->getPrimitiveSizeInBits() ==
1457 getOperand(0)->getType()->getPrimitiveSizeInBits();
1458 }
1459}
1460
1461/// This function determines if a pair of casts can be eliminated and what
1462/// opcode should be used in the elimination. This assumes that there are two
1463/// instructions like this:
1464/// * %F = firstOpcode SrcTy %x to MidTy
1465/// * %S = secondOpcode MidTy %F to DstTy
1466/// The function returns a resultOpcode so these two casts can be replaced with:
1467/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1468/// If no such cast is permited, the function returns 0.
1469unsigned CastInst::isEliminableCastPair(
1470 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1471 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1472{
1473 // Define the 144 possibilities for these two cast instructions. The values
1474 // in this matrix determine what to do in a given situation and select the
1475 // case in the switch below. The rows correspond to firstOp, the columns
1476 // correspond to secondOp. In looking at the table below, keep in mind
1477 // the following cast properties:
1478 //
1479 // Size Compare Source Destination
1480 // Operator Src ? Size Type Sign Type Sign
1481 // -------- ------------ ------------------- ---------------------
1482 // TRUNC > Integer Any Integral Any
1483 // ZEXT < Integral Unsigned Integer Any
1484 // SEXT < Integral Signed Integer Any
1485 // FPTOUI n/a FloatPt n/a Integral Unsigned
1486 // FPTOSI n/a FloatPt n/a Integral Signed
1487 // UITOFP n/a Integral Unsigned FloatPt n/a
1488 // SITOFP n/a Integral Signed FloatPt n/a
1489 // FPTRUNC > FloatPt n/a FloatPt n/a
1490 // FPEXT < FloatPt n/a FloatPt n/a
1491 // PTRTOINT n/a Pointer n/a Integral Unsigned
1492 // INTTOPTR n/a Integral Unsigned Pointer n/a
1493 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001494 //
1495 // NOTE: some transforms are safe, but we consider them to be non-profitable.
1496 // For example, we could merge "fptoui double to uint" + "zext uint to ulong",
1497 // into "fptoui double to ulong", but this loses information about the range
1498 // of the produced value (we no longer know the top-part is all zeros).
1499 // Further this conversion is often much more expensive for typical hardware,
1500 // and causes issues when building libgcc. We disallow fptosi+sext for the
1501 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001502 const unsigned numCastOps =
1503 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1504 static const uint8_t CastResults[numCastOps][numCastOps] = {
1505 // T F F U S F F P I B -+
1506 // R Z S P P I I T P 2 N T |
1507 // U E E 2 2 2 2 R E I T C +- secondOp
1508 // N X X U S F F N X N 2 V |
1509 // C T T I I P P C T T P T -+
1510 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1511 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1512 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001513 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1514 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001515 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1516 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1517 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1518 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1519 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1520 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1521 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1522 };
1523
1524 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1525 [secondOp-Instruction::CastOpsBegin];
1526 switch (ElimCase) {
1527 case 0:
1528 // categorically disallowed
1529 return 0;
1530 case 1:
1531 // allowed, use first cast's opcode
1532 return firstOp;
1533 case 2:
1534 // allowed, use second cast's opcode
1535 return secondOp;
1536 case 3:
1537 // no-op cast in second op implies firstOp as long as the DestTy
1538 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00001539 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001540 return firstOp;
1541 return 0;
1542 case 4:
1543 // no-op cast in second op implies firstOp as long as the DestTy
1544 // is floating point
1545 if (DstTy->isFloatingPoint())
1546 return firstOp;
1547 return 0;
1548 case 5:
1549 // no-op cast in first op implies secondOp as long as the SrcTy
1550 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00001551 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001552 return secondOp;
1553 return 0;
1554 case 6:
1555 // no-op cast in first op implies secondOp as long as the SrcTy
1556 // is a floating point
1557 if (SrcTy->isFloatingPoint())
1558 return secondOp;
1559 return 0;
1560 case 7: {
1561 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
1562 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1563 unsigned MidSize = MidTy->getPrimitiveSizeInBits();
1564 if (MidSize >= PtrSize)
1565 return Instruction::BitCast;
1566 return 0;
1567 }
1568 case 8: {
1569 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
1570 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
1571 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
1572 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1573 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1574 if (SrcSize == DstSize)
1575 return Instruction::BitCast;
1576 else if (SrcSize < DstSize)
1577 return firstOp;
1578 return secondOp;
1579 }
1580 case 9: // zext, sext -> zext, because sext can't sign extend after zext
1581 return Instruction::ZExt;
1582 case 10:
1583 // fpext followed by ftrunc is allowed if the bit size returned to is
1584 // the same as the original, in which case its just a bitcast
1585 if (SrcTy == DstTy)
1586 return Instruction::BitCast;
1587 return 0; // If the types are not the same we can't eliminate it.
1588 case 11:
1589 // bitcast followed by ptrtoint is allowed as long as the bitcast
1590 // is a pointer to pointer cast.
1591 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
1592 return secondOp;
1593 return 0;
1594 case 12:
1595 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
1596 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
1597 return firstOp;
1598 return 0;
1599 case 13: {
1600 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
1601 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1602 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1603 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1604 if (SrcSize <= PtrSize && SrcSize == DstSize)
1605 return Instruction::BitCast;
1606 return 0;
1607 }
1608 case 99:
1609 // cast combination can't happen (error in input). This is for all cases
1610 // where the MidTy is not the same for the two cast instructions.
1611 assert(!"Invalid Cast Combination");
1612 return 0;
1613 default:
1614 assert(!"Error in CastResults table!!!");
1615 return 0;
1616 }
1617 return 0;
1618}
1619
1620CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
1621 const std::string &Name, Instruction *InsertBefore) {
1622 // Construct and return the appropriate CastInst subclass
1623 switch (op) {
1624 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
1625 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
1626 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
1627 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
1628 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
1629 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
1630 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
1631 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
1632 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
1633 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
1634 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
1635 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
1636 default:
1637 assert(!"Invalid opcode provided");
1638 }
1639 return 0;
1640}
1641
1642CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
1643 const std::string &Name, BasicBlock *InsertAtEnd) {
1644 // Construct and return the appropriate CastInst subclass
1645 switch (op) {
1646 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
1647 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
1648 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
1649 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
1650 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
1651 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
1652 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
1653 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
1654 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
1655 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
1656 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
1657 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
1658 default:
1659 assert(!"Invalid opcode provided");
1660 }
1661 return 0;
1662}
1663
Reid Spencer5c140882006-12-04 20:17:56 +00001664CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
1665 const std::string &Name,
1666 Instruction *InsertBefore) {
1667 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1668 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1669 return create(Instruction::ZExt, S, Ty, Name, InsertBefore);
1670}
1671
1672CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
1673 const std::string &Name,
1674 BasicBlock *InsertAtEnd) {
1675 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1676 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1677 return create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
1678}
1679
1680CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
1681 const std::string &Name,
1682 Instruction *InsertBefore) {
1683 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1684 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1685 return create(Instruction::SExt, S, Ty, Name, InsertBefore);
1686}
1687
1688CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
1689 const std::string &Name,
1690 BasicBlock *InsertAtEnd) {
1691 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1692 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1693 return create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
1694}
1695
1696CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
1697 const std::string &Name,
1698 Instruction *InsertBefore) {
1699 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1700 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1701 return create(Instruction::Trunc, S, Ty, Name, InsertBefore);
1702}
1703
1704CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
1705 const std::string &Name,
1706 BasicBlock *InsertAtEnd) {
1707 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1708 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1709 return create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
1710}
1711
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001712CastInst *CastInst::createPointerCast(Value *S, const Type *Ty,
1713 const std::string &Name,
1714 BasicBlock *InsertAtEnd) {
1715 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001716 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001717 "Invalid cast");
1718
Chris Lattner03c49532007-01-15 02:27:26 +00001719 if (Ty->isInteger())
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001720 return create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
1721 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1722}
1723
1724/// @brief Create a BitCast or a PtrToInt cast instruction
1725CastInst *CastInst::createPointerCast(Value *S, const Type *Ty,
1726 const std::string &Name,
1727 Instruction *InsertBefore) {
1728 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001729 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001730 "Invalid cast");
1731
Chris Lattner03c49532007-01-15 02:27:26 +00001732 if (Ty->isInteger())
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001733 return create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
1734 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1735}
1736
Reid Spencer7e933472006-12-12 00:49:44 +00001737CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty,
1738 bool isSigned, const std::string &Name,
1739 Instruction *InsertBefore) {
Chris Lattner03c49532007-01-15 02:27:26 +00001740 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001741 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1742 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1743 Instruction::CastOps opcode =
1744 (SrcBits == DstBits ? Instruction::BitCast :
1745 (SrcBits > DstBits ? Instruction::Trunc :
1746 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1747 return create(opcode, C, Ty, Name, InsertBefore);
1748}
1749
1750CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty,
1751 bool isSigned, const std::string &Name,
1752 BasicBlock *InsertAtEnd) {
Chris Lattner03c49532007-01-15 02:27:26 +00001753 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001754 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1755 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1756 Instruction::CastOps opcode =
1757 (SrcBits == DstBits ? Instruction::BitCast :
1758 (SrcBits > DstBits ? Instruction::Trunc :
1759 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1760 return create(opcode, C, Ty, Name, InsertAtEnd);
1761}
1762
1763CastInst *CastInst::createFPCast(Value *C, const Type *Ty,
1764 const std::string &Name,
1765 Instruction *InsertBefore) {
1766 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1767 "Invalid cast");
1768 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1769 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1770 Instruction::CastOps opcode =
1771 (SrcBits == DstBits ? Instruction::BitCast :
1772 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
1773 return create(opcode, C, Ty, Name, InsertBefore);
1774}
1775
1776CastInst *CastInst::createFPCast(Value *C, const Type *Ty,
1777 const std::string &Name,
1778 BasicBlock *InsertAtEnd) {
1779 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1780 "Invalid cast");
1781 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1782 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1783 Instruction::CastOps opcode =
1784 (SrcBits == DstBits ? Instruction::BitCast :
1785 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
1786 return create(opcode, C, Ty, Name, InsertAtEnd);
1787}
1788
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001789// Provide a way to get a "cast" where the cast opcode is inferred from the
1790// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001791// logic in the castIsValid function below. This axiom should hold:
1792// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
1793// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001794// casting opcode for the arguments passed to it.
1795Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00001796CastInst::getCastOpcode(
1797 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001798 // Get the bit sizes, we'll need these
1799 const Type *SrcTy = Src->getType();
Dan Gohmanfead7972007-05-11 21:43:24 +00001800 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
1801 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001802
1803 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00001804 if (DestTy->isInteger()) { // Casting to integral
1805 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001806 if (DestBits < SrcBits)
1807 return Trunc; // int -> smaller int
1808 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00001809 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001810 return SExt; // signed -> SEXT
1811 else
1812 return ZExt; // unsigned -> ZEXT
1813 } else {
1814 return BitCast; // Same size, No-op cast
1815 }
1816 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00001817 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001818 return FPToSI; // FP -> sint
1819 else
1820 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00001821 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001822 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00001823 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001824 return BitCast; // Same size, no-op cast
1825 } else {
1826 assert(isa<PointerType>(SrcTy) &&
1827 "Casting from a value that is not first-class type");
1828 return PtrToInt; // ptr -> int
1829 }
1830 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00001831 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00001832 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001833 return SIToFP; // sint -> FP
1834 else
1835 return UIToFP; // uint -> FP
1836 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
1837 if (DestBits < SrcBits) {
1838 return FPTrunc; // FP -> smaller FP
1839 } else if (DestBits > SrcBits) {
1840 return FPExt; // FP -> larger FP
1841 } else {
1842 return BitCast; // same size, no-op cast
1843 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00001844 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001845 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00001846 "Casting vector to floating point of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001847 return BitCast; // same size, no-op cast
1848 } else {
1849 assert(0 && "Casting pointer or non-first class to float");
1850 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00001851 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
1852 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001853 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00001854 "Casting vector to vector of different widths");
1855 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001856 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00001857 return BitCast; // float/int -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001858 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00001859 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001860 }
1861 } else if (isa<PointerType>(DestTy)) {
1862 if (isa<PointerType>(SrcTy)) {
1863 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00001864 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001865 return IntToPtr; // int -> ptr
1866 } else {
1867 assert(!"Casting pointer to other than pointer or int");
1868 }
1869 } else {
1870 assert(!"Casting to type that is not first-class");
1871 }
1872
1873 // If we fall through to here we probably hit an assertion cast above
1874 // and assertions are not turned on. Anything we return is an error, so
1875 // BitCast is as good a choice as any.
1876 return BitCast;
1877}
1878
1879//===----------------------------------------------------------------------===//
1880// CastInst SubClass Constructors
1881//===----------------------------------------------------------------------===//
1882
1883/// Check that the construction parameters for a CastInst are correct. This
1884/// could be broken out into the separate constructors but it is useful to have
1885/// it in one place and to eliminate the redundant code for getting the sizes
1886/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001887bool
1888CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001889
1890 // Check for type sanity on the arguments
1891 const Type *SrcTy = S->getType();
1892 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
1893 return false;
1894
1895 // Get the size of the types in bits, we'll need this later
1896 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
1897 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
1898
1899 // Switch on the opcode provided
1900 switch (op) {
1901 default: return false; // This is an input error
1902 case Instruction::Trunc:
Chris Lattner03c49532007-01-15 02:27:26 +00001903 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001904 case Instruction::ZExt:
Chris Lattner03c49532007-01-15 02:27:26 +00001905 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001906 case Instruction::SExt:
Chris Lattner03c49532007-01-15 02:27:26 +00001907 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001908 case Instruction::FPTrunc:
1909 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
1910 SrcBitSize > DstBitSize;
1911 case Instruction::FPExt:
1912 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
1913 SrcBitSize < DstBitSize;
1914 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001915 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00001916 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
1917 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
1918 return SVTy->getElementType()->isInteger() &&
1919 DVTy->getElementType()->isFloatingPoint() &&
1920 SVTy->getNumElements() == DVTy->getNumElements();
1921 }
1922 }
Chris Lattner03c49532007-01-15 02:27:26 +00001923 return SrcTy->isInteger() && DstTy->isFloatingPoint();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001924 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001925 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00001926 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
1927 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
1928 return SVTy->getElementType()->isFloatingPoint() &&
1929 DVTy->getElementType()->isInteger() &&
1930 SVTy->getNumElements() == DVTy->getNumElements();
1931 }
1932 }
Chris Lattner03c49532007-01-15 02:27:26 +00001933 return SrcTy->isFloatingPoint() && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001934 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00001935 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001936 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00001937 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001938 case Instruction::BitCast:
1939 // BitCast implies a no-op cast of type only. No bits change.
1940 // However, you can't cast pointers to anything but pointers.
1941 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
1942 return false;
1943
1944 // Now we know we're not dealing with a pointer/non-poiner mismatch. In all
1945 // these cases, the cast is okay if the source and destination bit widths
1946 // are identical.
1947 return SrcBitSize == DstBitSize;
1948 }
1949}
1950
1951TruncInst::TruncInst(
1952 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1953) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001954 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001955}
1956
1957TruncInst::TruncInst(
1958 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1959) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001960 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001961}
1962
1963ZExtInst::ZExtInst(
1964 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1965) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001966 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001967}
1968
1969ZExtInst::ZExtInst(
1970 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1971) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001972 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001973}
1974SExtInst::SExtInst(
1975 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1976) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001977 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001978}
1979
Jeff Cohencc08c832006-12-02 02:22:01 +00001980SExtInst::SExtInst(
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001981 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1982) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001983 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001984}
1985
1986FPTruncInst::FPTruncInst(
1987 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1988) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001989 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001990}
1991
1992FPTruncInst::FPTruncInst(
1993 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1994) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001995 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001996}
1997
1998FPExtInst::FPExtInst(
1999 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2000) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002001 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002002}
2003
2004FPExtInst::FPExtInst(
2005 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2006) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002007 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002008}
2009
2010UIToFPInst::UIToFPInst(
2011 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2012) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002013 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002014}
2015
2016UIToFPInst::UIToFPInst(
2017 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2018) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002019 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002020}
2021
2022SIToFPInst::SIToFPInst(
2023 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2024) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002025 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002026}
2027
2028SIToFPInst::SIToFPInst(
2029 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2030) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002031 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002032}
2033
2034FPToUIInst::FPToUIInst(
2035 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2036) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002037 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002038}
2039
2040FPToUIInst::FPToUIInst(
2041 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2042) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002043 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002044}
2045
2046FPToSIInst::FPToSIInst(
2047 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2048) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002049 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002050}
2051
2052FPToSIInst::FPToSIInst(
2053 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2054) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002055 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002056}
2057
2058PtrToIntInst::PtrToIntInst(
2059 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2060) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002061 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002062}
2063
2064PtrToIntInst::PtrToIntInst(
2065 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2066) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002067 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002068}
2069
2070IntToPtrInst::IntToPtrInst(
2071 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2072) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002073 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002074}
2075
2076IntToPtrInst::IntToPtrInst(
2077 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2078) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002079 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002080}
2081
2082BitCastInst::BitCastInst(
2083 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2084) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002085 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002086}
2087
2088BitCastInst::BitCastInst(
2089 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2090) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002091 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002092}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002093
2094//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002095// CmpInst Classes
2096//===----------------------------------------------------------------------===//
2097
2098CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
2099 const std::string &Name, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +00002100 : Instruction(Type::Int1Ty, op, Ops, 2, InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002101 Ops[0].init(LHS, this);
2102 Ops[1].init(RHS, this);
2103 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002104 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002105 if (op == Instruction::ICmp) {
2106 assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
2107 predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
2108 "Invalid ICmp predicate value");
2109 const Type* Op0Ty = getOperand(0)->getType();
2110 const Type* Op1Ty = getOperand(1)->getType();
2111 assert(Op0Ty == Op1Ty &&
2112 "Both operands to ICmp instruction are not of the same type!");
2113 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002114 assert((Op0Ty->isInteger() || isa<PointerType>(Op0Ty)) &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002115 "Invalid operand types for ICmp instruction");
2116 return;
2117 }
2118 assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
2119 assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
2120 "Invalid FCmp predicate value");
2121 const Type* Op0Ty = getOperand(0)->getType();
2122 const Type* Op1Ty = getOperand(1)->getType();
2123 assert(Op0Ty == Op1Ty &&
2124 "Both operands to FCmp instruction are not of the same type!");
2125 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002126 assert(Op0Ty->isFloatingPoint() &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002127 "Invalid operand types for FCmp instruction");
2128}
2129
2130CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
2131 const std::string &Name, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +00002132 : Instruction(Type::Int1Ty, op, Ops, 2, InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002133 Ops[0].init(LHS, this);
2134 Ops[1].init(RHS, this);
2135 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002136 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002137 if (op == Instruction::ICmp) {
2138 assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
2139 predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
2140 "Invalid ICmp predicate value");
2141
2142 const Type* Op0Ty = getOperand(0)->getType();
2143 const Type* Op1Ty = getOperand(1)->getType();
2144 assert(Op0Ty == Op1Ty &&
2145 "Both operands to ICmp instruction are not of the same type!");
2146 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002147 assert(Op0Ty->isInteger() || isa<PointerType>(Op0Ty) &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002148 "Invalid operand types for ICmp instruction");
2149 return;
2150 }
2151 assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
2152 assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
2153 "Invalid FCmp predicate value");
2154 const Type* Op0Ty = getOperand(0)->getType();
2155 const Type* Op1Ty = getOperand(1)->getType();
2156 assert(Op0Ty == Op1Ty &&
2157 "Both operands to FCmp instruction are not of the same type!");
2158 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002159 assert(Op0Ty->isFloatingPoint() &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002160 "Invalid operand types for FCmp instruction");
2161}
2162
2163CmpInst *
2164CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2165 const std::string &Name, Instruction *InsertBefore) {
2166 if (Op == Instruction::ICmp) {
2167 return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name,
2168 InsertBefore);
2169 }
2170 return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name,
2171 InsertBefore);
2172}
2173
2174CmpInst *
2175CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2176 const std::string &Name, BasicBlock *InsertAtEnd) {
2177 if (Op == Instruction::ICmp) {
2178 return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name,
2179 InsertAtEnd);
2180 }
2181 return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name,
2182 InsertAtEnd);
2183}
2184
2185void CmpInst::swapOperands() {
2186 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2187 IC->swapOperands();
2188 else
2189 cast<FCmpInst>(this)->swapOperands();
2190}
2191
2192bool CmpInst::isCommutative() {
2193 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2194 return IC->isCommutative();
2195 return cast<FCmpInst>(this)->isCommutative();
2196}
2197
2198bool CmpInst::isEquality() {
2199 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2200 return IC->isEquality();
2201 return cast<FCmpInst>(this)->isEquality();
2202}
2203
2204
2205ICmpInst::Predicate ICmpInst::getInversePredicate(Predicate pred) {
2206 switch (pred) {
2207 default:
2208 assert(!"Unknown icmp predicate!");
2209 case ICMP_EQ: return ICMP_NE;
2210 case ICMP_NE: return ICMP_EQ;
2211 case ICMP_UGT: return ICMP_ULE;
2212 case ICMP_ULT: return ICMP_UGE;
2213 case ICMP_UGE: return ICMP_ULT;
2214 case ICMP_ULE: return ICMP_UGT;
2215 case ICMP_SGT: return ICMP_SLE;
2216 case ICMP_SLT: return ICMP_SGE;
2217 case ICMP_SGE: return ICMP_SLT;
2218 case ICMP_SLE: return ICMP_SGT;
2219 }
2220}
2221
2222ICmpInst::Predicate ICmpInst::getSwappedPredicate(Predicate pred) {
2223 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002224 default: assert(! "Unknown icmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002225 case ICMP_EQ: case ICMP_NE:
2226 return pred;
2227 case ICMP_SGT: return ICMP_SLT;
2228 case ICMP_SLT: return ICMP_SGT;
2229 case ICMP_SGE: return ICMP_SLE;
2230 case ICMP_SLE: return ICMP_SGE;
2231 case ICMP_UGT: return ICMP_ULT;
2232 case ICMP_ULT: return ICMP_UGT;
2233 case ICMP_UGE: return ICMP_ULE;
2234 case ICMP_ULE: return ICMP_UGE;
2235 }
2236}
2237
Reid Spencer266e42b2006-12-23 06:05:41 +00002238ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2239 switch (pred) {
2240 default: assert(! "Unknown icmp predicate!");
2241 case ICMP_EQ: case ICMP_NE:
2242 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2243 return pred;
2244 case ICMP_UGT: return ICMP_SGT;
2245 case ICMP_ULT: return ICMP_SLT;
2246 case ICMP_UGE: return ICMP_SGE;
2247 case ICMP_ULE: return ICMP_SLE;
2248 }
2249}
2250
2251bool ICmpInst::isSignedPredicate(Predicate pred) {
2252 switch (pred) {
2253 default: assert(! "Unknown icmp predicate!");
2254 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2255 return true;
2256 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2257 case ICMP_UGE: case ICMP_ULE:
2258 return false;
2259 }
2260}
2261
Reid Spencer0286bc12007-02-28 22:00:54 +00002262/// Initialize a set of values that all satisfy the condition with C.
2263///
2264ConstantRange
2265ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2266 APInt Lower(C);
2267 APInt Upper(C);
2268 uint32_t BitWidth = C.getBitWidth();
2269 switch (pred) {
2270 default: assert(0 && "Invalid ICmp opcode to ConstantRange ctor!");
2271 case ICmpInst::ICMP_EQ: Upper++; break;
2272 case ICmpInst::ICMP_NE: Lower++; break;
2273 case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
2274 case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
2275 case ICmpInst::ICMP_UGT:
2276 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2277 break;
2278 case ICmpInst::ICMP_SGT:
2279 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2280 break;
2281 case ICmpInst::ICMP_ULE:
2282 Lower = APInt::getMinValue(BitWidth); Upper++;
2283 break;
2284 case ICmpInst::ICMP_SLE:
2285 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2286 break;
2287 case ICmpInst::ICMP_UGE:
2288 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2289 break;
2290 case ICmpInst::ICMP_SGE:
2291 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2292 break;
2293 }
2294 return ConstantRange(Lower, Upper);
2295}
2296
Reid Spencerd9436b62006-11-20 01:22:35 +00002297FCmpInst::Predicate FCmpInst::getInversePredicate(Predicate pred) {
2298 switch (pred) {
2299 default:
2300 assert(!"Unknown icmp predicate!");
2301 case FCMP_OEQ: return FCMP_UNE;
2302 case FCMP_ONE: return FCMP_UEQ;
2303 case FCMP_OGT: return FCMP_ULE;
2304 case FCMP_OLT: return FCMP_UGE;
2305 case FCMP_OGE: return FCMP_ULT;
2306 case FCMP_OLE: return FCMP_UGT;
2307 case FCMP_UEQ: return FCMP_ONE;
2308 case FCMP_UNE: return FCMP_OEQ;
2309 case FCMP_UGT: return FCMP_OLE;
2310 case FCMP_ULT: return FCMP_OGE;
2311 case FCMP_UGE: return FCMP_OLT;
2312 case FCMP_ULE: return FCMP_OGT;
2313 case FCMP_ORD: return FCMP_UNO;
2314 case FCMP_UNO: return FCMP_ORD;
2315 case FCMP_TRUE: return FCMP_FALSE;
2316 case FCMP_FALSE: return FCMP_TRUE;
2317 }
2318}
2319
2320FCmpInst::Predicate FCmpInst::getSwappedPredicate(Predicate pred) {
2321 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002322 default: assert(!"Unknown fcmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002323 case FCMP_FALSE: case FCMP_TRUE:
2324 case FCMP_OEQ: case FCMP_ONE:
2325 case FCMP_UEQ: case FCMP_UNE:
2326 case FCMP_ORD: case FCMP_UNO:
2327 return pred;
2328 case FCMP_OGT: return FCMP_OLT;
2329 case FCMP_OLT: return FCMP_OGT;
2330 case FCMP_OGE: return FCMP_OLE;
2331 case FCMP_OLE: return FCMP_OGE;
2332 case FCMP_UGT: return FCMP_ULT;
2333 case FCMP_ULT: return FCMP_UGT;
2334 case FCMP_UGE: return FCMP_ULE;
2335 case FCMP_ULE: return FCMP_UGE;
2336 }
2337}
2338
Reid Spencer266e42b2006-12-23 06:05:41 +00002339bool CmpInst::isUnsigned(unsigned short predicate) {
2340 switch (predicate) {
2341 default: return false;
2342 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2343 case ICmpInst::ICMP_UGE: return true;
2344 }
2345}
2346
2347bool CmpInst::isSigned(unsigned short predicate){
2348 switch (predicate) {
2349 default: return false;
2350 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2351 case ICmpInst::ICMP_SGE: return true;
2352 }
2353}
2354
2355bool CmpInst::isOrdered(unsigned short predicate) {
2356 switch (predicate) {
2357 default: return false;
2358 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2359 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2360 case FCmpInst::FCMP_ORD: return true;
2361 }
2362}
2363
2364bool CmpInst::isUnordered(unsigned short predicate) {
2365 switch (predicate) {
2366 default: return false;
2367 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2368 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2369 case FCmpInst::FCMP_UNO: return true;
2370 }
2371}
2372
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002373//===----------------------------------------------------------------------===//
2374// SwitchInst Implementation
2375//===----------------------------------------------------------------------===//
2376
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002377void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002378 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002379 ReservedSpace = 2+NumCases*2;
2380 NumOperands = 2;
2381 OperandList = new Use[ReservedSpace];
2382
2383 OperandList[0].init(Value, this);
2384 OperandList[1].init(Default, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002385}
2386
Chris Lattner2195fc42007-02-24 00:55:48 +00002387/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2388/// switch on and a default destination. The number of additional cases can
2389/// be specified here to make memory allocation more efficient. This
2390/// constructor can also autoinsert before another instruction.
2391SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2392 Instruction *InsertBefore)
2393 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertBefore) {
2394 init(Value, Default, NumCases);
2395}
2396
2397/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2398/// switch on and a default destination. The number of additional cases can
2399/// be specified here to make memory allocation more efficient. This
2400/// constructor also autoinserts at the end of the specified BasicBlock.
2401SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2402 BasicBlock *InsertAtEnd)
2403 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertAtEnd) {
2404 init(Value, Default, NumCases);
2405}
2406
Misha Brukmanb1c93172005-04-21 23:48:37 +00002407SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattner2195fc42007-02-24 00:55:48 +00002408 : TerminatorInst(Type::VoidTy, Instruction::Switch,
2409 new Use[SI.getNumOperands()], SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002410 Use *OL = OperandList, *InOL = SI.OperandList;
2411 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
2412 OL[i].init(InOL[i], this);
2413 OL[i+1].init(InOL[i+1], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002414 }
2415}
2416
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002417SwitchInst::~SwitchInst() {
2418 delete [] OperandList;
2419}
2420
2421
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002422/// addCase - Add an entry to the switch instruction...
2423///
Chris Lattner47ac1872005-02-24 05:32:09 +00002424void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002425 unsigned OpNo = NumOperands;
2426 if (OpNo+2 > ReservedSpace)
2427 resizeOperands(0); // Get more space!
2428 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002429 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002430 NumOperands = OpNo+2;
2431 OperandList[OpNo].init(OnVal, this);
2432 OperandList[OpNo+1].init(Dest, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002433}
2434
2435/// removeCase - This method removes the specified successor from the switch
2436/// instruction. Note that this cannot be used to remove the default
2437/// destination (successor #0).
2438///
2439void SwitchInst::removeCase(unsigned idx) {
2440 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002441 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2442
2443 unsigned NumOps = getNumOperands();
2444 Use *OL = OperandList;
2445
2446 // Move everything after this operand down.
2447 //
2448 // FIXME: we could just swap with the end of the list, then erase. However,
2449 // client might not expect this to happen. The code as it is thrashes the
2450 // use/def lists, which is kinda lame.
2451 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
2452 OL[i-2] = OL[i];
2453 OL[i-2+1] = OL[i+1];
2454 }
2455
2456 // Nuke the last value.
2457 OL[NumOps-2].set(0);
2458 OL[NumOps-2+1].set(0);
2459 NumOperands = NumOps-2;
2460}
2461
2462/// resizeOperands - resize operands - This adjusts the length of the operands
2463/// list according to the following behavior:
2464/// 1. If NumOps == 0, grow the operand list in response to a push_back style
2465/// of operation. This grows the number of ops by 1.5 times.
2466/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
2467/// 3. If NumOps == NumOperands, trim the reserved space.
2468///
2469void SwitchInst::resizeOperands(unsigned NumOps) {
2470 if (NumOps == 0) {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002471 NumOps = getNumOperands()/2*6;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002472 } else if (NumOps*2 > NumOperands) {
2473 // No resize needed.
2474 if (ReservedSpace >= NumOps) return;
2475 } else if (NumOps == NumOperands) {
2476 if (ReservedSpace == NumOps) return;
2477 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002478 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002479 }
2480
2481 ReservedSpace = NumOps;
2482 Use *NewOps = new Use[NumOps];
2483 Use *OldOps = OperandList;
2484 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2485 NewOps[i].init(OldOps[i], this);
2486 OldOps[i].set(0);
2487 }
2488 delete [] OldOps;
2489 OperandList = NewOps;
2490}
2491
2492
2493BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
2494 return getSuccessor(idx);
2495}
2496unsigned SwitchInst::getNumSuccessorsV() const {
2497 return getNumSuccessors();
2498}
2499void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
2500 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002501}
Chris Lattnerf22be932004-10-15 23:52:53 +00002502
2503
2504// Define these methods here so vtables don't get emitted into every translation
2505// unit that uses these classes.
2506
2507GetElementPtrInst *GetElementPtrInst::clone() const {
2508 return new GetElementPtrInst(*this);
2509}
2510
2511BinaryOperator *BinaryOperator::clone() const {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002512 return create(getOpcode(), Ops[0], Ops[1]);
Chris Lattnerf22be932004-10-15 23:52:53 +00002513}
2514
Chris Lattner0b490b02007-08-24 20:48:18 +00002515FCmpInst* FCmpInst::clone() const {
2516 return new FCmpInst(getPredicate(), Ops[0], Ops[1]);
2517}
2518ICmpInst* ICmpInst::clone() const {
2519 return new ICmpInst(getPredicate(), Ops[0], Ops[1]);
Reid Spencerd9436b62006-11-20 01:22:35 +00002520}
2521
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002522MallocInst *MallocInst::clone() const { return new MallocInst(*this); }
2523AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); }
2524FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); }
2525LoadInst *LoadInst::clone() const { return new LoadInst(*this); }
2526StoreInst *StoreInst::clone() const { return new StoreInst(*this); }
2527CastInst *TruncInst::clone() const { return new TruncInst(*this); }
2528CastInst *ZExtInst::clone() const { return new ZExtInst(*this); }
2529CastInst *SExtInst::clone() const { return new SExtInst(*this); }
2530CastInst *FPTruncInst::clone() const { return new FPTruncInst(*this); }
2531CastInst *FPExtInst::clone() const { return new FPExtInst(*this); }
2532CastInst *UIToFPInst::clone() const { return new UIToFPInst(*this); }
2533CastInst *SIToFPInst::clone() const { return new SIToFPInst(*this); }
2534CastInst *FPToUIInst::clone() const { return new FPToUIInst(*this); }
2535CastInst *FPToSIInst::clone() const { return new FPToSIInst(*this); }
2536CastInst *PtrToIntInst::clone() const { return new PtrToIntInst(*this); }
2537CastInst *IntToPtrInst::clone() const { return new IntToPtrInst(*this); }
2538CastInst *BitCastInst::clone() const { return new BitCastInst(*this); }
2539CallInst *CallInst::clone() const { return new CallInst(*this); }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002540SelectInst *SelectInst::clone() const { return new SelectInst(*this); }
2541VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); }
2542
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002543ExtractElementInst *ExtractElementInst::clone() const {
2544 return new ExtractElementInst(*this);
2545}
2546InsertElementInst *InsertElementInst::clone() const {
2547 return new InsertElementInst(*this);
2548}
2549ShuffleVectorInst *ShuffleVectorInst::clone() const {
2550 return new ShuffleVectorInst(*this);
2551}
Chris Lattnerf22be932004-10-15 23:52:53 +00002552PHINode *PHINode::clone() const { return new PHINode(*this); }
2553ReturnInst *ReturnInst::clone() const { return new ReturnInst(*this); }
2554BranchInst *BranchInst::clone() const { return new BranchInst(*this); }
2555SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); }
2556InvokeInst *InvokeInst::clone() const { return new InvokeInst(*this); }
2557UnwindInst *UnwindInst::clone() const { return new UnwindInst(); }
Chris Lattner5e0b9f22004-10-16 18:08:06 +00002558UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();}