blob: 2b57af85272dc0b4c273fddd7218337b50a9fd17 [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"
20#include "llvm/Support/CallSite.h"
21using namespace llvm;
22
Chris Lattnerf7b6d312005-05-06 20:26:43 +000023unsigned CallSite::getCallingConv() const {
24 if (CallInst *CI = dyn_cast<CallInst>(I))
25 return CI->getCallingConv();
26 else
27 return cast<InvokeInst>(I)->getCallingConv();
28}
29void CallSite::setCallingConv(unsigned CC) {
30 if (CallInst *CI = dyn_cast<CallInst>(I))
31 CI->setCallingConv(CC);
32 else
33 cast<InvokeInst>(I)->setCallingConv(CC);
34}
35
36
Chris Lattner1c12a882006-06-21 16:53:47 +000037
38
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000039//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000040// TerminatorInst Class
41//===----------------------------------------------------------------------===//
42
43TerminatorInst::TerminatorInst(Instruction::TermOps iType,
Misha Brukmanb1c93172005-04-21 23:48:37 +000044 Use *Ops, unsigned NumOps, Instruction *IB)
Chris Lattnerafdb3de2005-01-29 00:35:16 +000045 : Instruction(Type::VoidTy, iType, Ops, NumOps, "", IB) {
46}
47
48TerminatorInst::TerminatorInst(Instruction::TermOps iType,
49 Use *Ops, unsigned NumOps, BasicBlock *IAE)
50 : Instruction(Type::VoidTy, iType, Ops, NumOps, "", IAE) {
51}
52
Chris Lattner1c12a882006-06-21 16:53:47 +000053// Out of line virtual method, so the vtable, etc has a home.
54TerminatorInst::~TerminatorInst() {
55}
56
57// Out of line virtual method, so the vtable, etc has a home.
58UnaryInstruction::~UnaryInstruction() {
59}
Chris Lattnerafdb3de2005-01-29 00:35:16 +000060
61
62//===----------------------------------------------------------------------===//
63// PHINode Class
64//===----------------------------------------------------------------------===//
65
66PHINode::PHINode(const PHINode &PN)
67 : Instruction(PN.getType(), Instruction::PHI,
68 new Use[PN.getNumOperands()], PN.getNumOperands()),
69 ReservedSpace(PN.getNumOperands()) {
70 Use *OL = OperandList;
71 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
72 OL[i].init(PN.getOperand(i), this);
73 OL[i+1].init(PN.getOperand(i+1), this);
74 }
75}
76
77PHINode::~PHINode() {
78 delete [] OperandList;
79}
80
81// removeIncomingValue - Remove an incoming value. This is useful if a
82// predecessor basic block is deleted.
83Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
84 unsigned NumOps = getNumOperands();
85 Use *OL = OperandList;
86 assert(Idx*2 < NumOps && "BB not in PHI node!");
87 Value *Removed = OL[Idx*2];
88
89 // Move everything after this operand down.
90 //
91 // FIXME: we could just swap with the end of the list, then erase. However,
92 // client might not expect this to happen. The code as it is thrashes the
93 // use/def lists, which is kinda lame.
94 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
95 OL[i-2] = OL[i];
96 OL[i-2+1] = OL[i+1];
97 }
98
99 // Nuke the last value.
100 OL[NumOps-2].set(0);
101 OL[NumOps-2+1].set(0);
102 NumOperands = NumOps-2;
103
104 // If the PHI node is dead, because it has zero entries, nuke it now.
105 if (NumOps == 2 && DeletePHIIfEmpty) {
106 // If anyone is using this PHI, make them use a dummy value instead...
107 replaceAllUsesWith(UndefValue::get(getType()));
108 eraseFromParent();
109 }
110 return Removed;
111}
112
113/// resizeOperands - resize operands - This adjusts the length of the operands
114/// list according to the following behavior:
115/// 1. If NumOps == 0, grow the operand list in response to a push_back style
116/// of operation. This grows the number of ops by 1.5 times.
117/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
118/// 3. If NumOps == NumOperands, trim the reserved space.
119///
120void PHINode::resizeOperands(unsigned NumOps) {
121 if (NumOps == 0) {
122 NumOps = (getNumOperands())*3/2;
123 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
124 } else if (NumOps*2 > NumOperands) {
125 // No resize needed.
126 if (ReservedSpace >= NumOps) return;
127 } else if (NumOps == NumOperands) {
128 if (ReservedSpace == NumOps) return;
129 } else {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000130 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000131 }
132
133 ReservedSpace = NumOps;
134 Use *NewOps = new Use[NumOps];
135 Use *OldOps = OperandList;
136 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
137 NewOps[i].init(OldOps[i], this);
138 OldOps[i].set(0);
139 }
140 delete [] OldOps;
141 OperandList = NewOps;
142}
143
Nate Begemanb3923212005-08-04 23:24:19 +0000144/// hasConstantValue - If the specified PHI node always merges together the same
145/// value, return the value, otherwise return null.
146///
Chris Lattner1d8b2482005-08-05 00:49:06 +0000147Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const {
Nate Begemanb3923212005-08-04 23:24:19 +0000148 // If the PHI node only has one incoming value, eliminate the PHI node...
149 if (getNumIncomingValues() == 1)
Chris Lattner6e709c12005-08-05 15:37:31 +0000150 if (getIncomingValue(0) != this) // not X = phi X
151 return getIncomingValue(0);
152 else
153 return UndefValue::get(getType()); // Self cycle is dead.
154
Nate Begemanb3923212005-08-04 23:24:19 +0000155 // Otherwise if all of the incoming values are the same for the PHI, replace
156 // the PHI node with the incoming value.
157 //
158 Value *InVal = 0;
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000159 bool HasUndefInput = false;
Nate Begemanb3923212005-08-04 23:24:19 +0000160 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000161 if (isa<UndefValue>(getIncomingValue(i)))
162 HasUndefInput = true;
163 else if (getIncomingValue(i) != this) // Not the PHI node itself...
Nate Begemanb3923212005-08-04 23:24:19 +0000164 if (InVal && getIncomingValue(i) != InVal)
165 return 0; // Not the same, bail out.
166 else
167 InVal = getIncomingValue(i);
168
169 // The only case that could cause InVal to be null is if we have a PHI node
170 // that only has entries for itself. In this case, there is no entry into the
171 // loop, so kill the PHI.
172 //
173 if (InVal == 0) InVal = UndefValue::get(getType());
174
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000175 // If we have a PHI node like phi(X, undef, X), where X is defined by some
176 // instruction, we cannot always return X as the result of the PHI node. Only
177 // do this if X is not an instruction (thus it must dominate the PHI block),
178 // or if the client is prepared to deal with this possibility.
179 if (HasUndefInput && !AllowNonDominatingInstruction)
180 if (Instruction *IV = dyn_cast<Instruction>(InVal))
181 // If it's in the entry block, it dominates everything.
Chris Lattner37774af2005-08-05 01:03:27 +0000182 if (IV->getParent() != &IV->getParent()->getParent()->front() ||
183 isa<InvokeInst>(IV))
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000184 return 0; // Cannot guarantee that InVal dominates this PHINode.
185
Nate Begemanb3923212005-08-04 23:24:19 +0000186 // All of the incoming values are the same, return the value now.
187 return InVal;
188}
189
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000190
191//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000192// CallInst Implementation
193//===----------------------------------------------------------------------===//
194
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000195CallInst::~CallInst() {
196 delete [] OperandList;
197}
198
Chris Lattner054ba2c2007-02-13 00:58:44 +0000199void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
200 NumOperands = NumParams+1;
201 Use *OL = OperandList = new Use[NumParams+1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000202 OL[0].init(Func, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000203
Misha Brukmanb1c93172005-04-21 23:48:37 +0000204 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000205 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000206 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000207
Chris Lattner054ba2c2007-02-13 00:58:44 +0000208 assert((NumParams == FTy->getNumParams() ||
209 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000210 "Calling a function with bad signature!");
Chris Lattner054ba2c2007-02-13 00:58:44 +0000211 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000212 assert((i >= FTy->getNumParams() ||
213 FTy->getParamType(i) == Params[i]->getType()) &&
214 "Calling a function with a bad signature!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000215 OL[i+1].init(Params[i], this);
Chris Lattner667a0562006-05-03 00:48:22 +0000216 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000217}
218
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000219void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
220 NumOperands = 3;
221 Use *OL = OperandList = new Use[3];
222 OL[0].init(Func, this);
223 OL[1].init(Actual1, this);
224 OL[2].init(Actual2, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000225
226 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000227 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000228 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000229
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000230 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000231 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000232 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000233 assert((0 >= FTy->getNumParams() ||
234 FTy->getParamType(0) == Actual1->getType()) &&
235 "Calling a function with a bad signature!");
236 assert((1 >= FTy->getNumParams() ||
237 FTy->getParamType(1) == Actual2->getType()) &&
238 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000239}
240
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000241void CallInst::init(Value *Func, Value *Actual) {
242 NumOperands = 2;
243 Use *OL = OperandList = new Use[2];
244 OL[0].init(Func, this);
245 OL[1].init(Actual, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000246
247 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000248 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000249 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000250
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000251 assert((FTy->getNumParams() == 1 ||
252 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000253 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000254 assert((0 == FTy->getNumParams() ||
255 FTy->getParamType(0) == Actual->getType()) &&
256 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000257}
258
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000259void CallInst::init(Value *Func) {
260 NumOperands = 1;
261 Use *OL = OperandList = new Use[1];
262 OL[0].init(Func, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000263
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000264 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000265 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000266 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000267
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000268 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000269}
270
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000271CallInst::CallInst(Value *Func, Value* const *Args, unsigned NumArgs,
Misha Brukmanb1c93172005-04-21 23:48:37 +0000272 const std::string &Name, BasicBlock *InsertAtEnd)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000273 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
274 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000275 Instruction::Call, 0, 0, Name, InsertAtEnd) {
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000276 init(Func, Args, NumArgs);
277}
278CallInst::CallInst(Value *Func, Value* const *Args, unsigned NumArgs,
279 const std::string &Name, Instruction *InsertBefore)
280: Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
281 ->getElementType())->getReturnType(),
282 Instruction::Call, 0, 0, Name, InsertBefore) {
283 init(Func, Args, NumArgs);
284}
285
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000286CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2,
287 const std::string &Name, Instruction *InsertBefore)
288 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
289 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000290 Instruction::Call, 0, 0, Name, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000291 init(Func, Actual1, Actual2);
292}
293
294CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2,
295 const std::string &Name, BasicBlock *InsertAtEnd)
296 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
297 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000298 Instruction::Call, 0, 0, Name, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000299 init(Func, Actual1, Actual2);
300}
301
302CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
303 Instruction *InsertBefore)
304 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
305 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000306 Instruction::Call, 0, 0, Name, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000307 init(Func, Actual);
308}
309
310CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
311 BasicBlock *InsertAtEnd)
312 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
313 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000314 Instruction::Call, 0, 0, Name, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000315 init(Func, Actual);
316}
317
318CallInst::CallInst(Value *Func, const std::string &Name,
319 Instruction *InsertBefore)
320 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
321 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000322 Instruction::Call, 0, 0, Name, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000323 init(Func);
324}
325
326CallInst::CallInst(Value *Func, const std::string &Name,
327 BasicBlock *InsertAtEnd)
328 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
329 ->getElementType())->getReturnType(),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000330 Instruction::Call, 0, 0, Name, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000331 init(Func);
332}
333
Misha Brukmanb1c93172005-04-21 23:48:37 +0000334CallInst::CallInst(const CallInst &CI)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000335 : Instruction(CI.getType(), Instruction::Call, new Use[CI.getNumOperands()],
336 CI.getNumOperands()) {
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000337 SubclassData = CI.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000338 Use *OL = OperandList;
339 Use *InOL = CI.OperandList;
340 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
341 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000342}
343
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000344
345//===----------------------------------------------------------------------===//
346// InvokeInst Implementation
347//===----------------------------------------------------------------------===//
348
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000349InvokeInst::~InvokeInst() {
350 delete [] OperandList;
351}
352
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000353void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000354 Value* const *Args, unsigned NumArgs) {
355 NumOperands = 3+NumArgs;
356 Use *OL = OperandList = new Use[3+NumArgs];
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000357 OL[0].init(Fn, this);
358 OL[1].init(IfNormal, this);
359 OL[2].init(IfException, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000360 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000361 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000362 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000363
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000364 assert((NumArgs == FTy->getNumParams()) ||
365 (FTy->isVarArg() && NumArgs > FTy->getNumParams()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000366 "Calling a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000367
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000368 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000369 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000370 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000371 "Invoking a function with a bad signature!");
372
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000373 OL[i+3].init(Args[i], this);
Chris Lattner667a0562006-05-03 00:48:22 +0000374 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000375}
376
377InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
378 BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000379 Value* const *Args, unsigned NumArgs,
380 const std::string &Name, Instruction *InsertBefore)
381 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
382 ->getElementType())->getReturnType(),
383 Instruction::Invoke, 0, 0, Name, InsertBefore) {
384 init(Fn, IfNormal, IfException, Args, NumArgs);
385}
386
387InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
388 BasicBlock *IfException,
389 Value* const *Args, unsigned NumArgs,
390 const std::string &Name, BasicBlock *InsertAtEnd)
391 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
392 ->getElementType())->getReturnType(),
393 Instruction::Invoke, 0, 0, Name, InsertAtEnd) {
394 init(Fn, IfNormal, IfException, Args, NumArgs);
395}
396
Misha Brukmanb1c93172005-04-21 23:48:37 +0000397InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000398 : TerminatorInst(II.getType(), Instruction::Invoke,
399 new Use[II.getNumOperands()], II.getNumOperands()) {
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000400 SubclassData = II.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000401 Use *OL = OperandList, *InOL = II.OperandList;
402 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
403 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000404}
405
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000406BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
407 return getSuccessor(idx);
408}
409unsigned InvokeInst::getNumSuccessorsV() const {
410 return getNumSuccessors();
411}
412void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
413 return setSuccessor(idx, B);
414}
415
416
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000417//===----------------------------------------------------------------------===//
418// ReturnInst Implementation
419//===----------------------------------------------------------------------===//
420
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000421void ReturnInst::init(Value *retVal) {
422 if (retVal && retVal->getType() != Type::VoidTy) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000423 assert(!isa<BasicBlock>(retVal) &&
Alkis Evlogimenos531e9012004-11-17 21:02:25 +0000424 "Cannot return basic block. Probably using the incorrect ctor");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000425 NumOperands = 1;
426 RetVal.init(retVal, this);
Alkis Evlogimenos531e9012004-11-17 21:02:25 +0000427 }
428}
429
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000430unsigned ReturnInst::getNumSuccessorsV() const {
431 return getNumSuccessors();
432}
433
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000434// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
435// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000436void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000437 assert(0 && "ReturnInst has no successors!");
438}
439
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000440BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
441 assert(0 && "ReturnInst has no successors!");
442 abort();
443 return 0;
444}
445
446
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000447//===----------------------------------------------------------------------===//
448// UnwindInst Implementation
449//===----------------------------------------------------------------------===//
450
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000451unsigned UnwindInst::getNumSuccessorsV() const {
452 return getNumSuccessors();
453}
454
455void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000456 assert(0 && "UnwindInst has no successors!");
457}
458
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000459BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
460 assert(0 && "UnwindInst has no successors!");
461 abort();
462 return 0;
463}
464
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000465//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000466// UnreachableInst Implementation
467//===----------------------------------------------------------------------===//
468
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000469unsigned UnreachableInst::getNumSuccessorsV() const {
470 return getNumSuccessors();
471}
472
473void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
474 assert(0 && "UnwindInst has no successors!");
475}
476
477BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
478 assert(0 && "UnwindInst has no successors!");
479 abort();
480 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000481}
482
483//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000484// BranchInst Implementation
485//===----------------------------------------------------------------------===//
486
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000487void BranchInst::AssertOK() {
488 if (isConditional())
Reid Spencer542964f2007-01-11 18:21:29 +0000489 assert(getCondition()->getType() == Type::Int1Ty &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000490 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000491}
492
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000493BranchInst::BranchInst(const BranchInst &BI) :
494 TerminatorInst(Instruction::Br, Ops, BI.getNumOperands()) {
495 OperandList[0].init(BI.getOperand(0), this);
496 if (BI.getNumOperands() != 1) {
497 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
498 OperandList[1].init(BI.getOperand(1), this);
499 OperandList[2].init(BI.getOperand(2), this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000500 }
501}
502
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000503BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
504 return getSuccessor(idx);
505}
506unsigned BranchInst::getNumSuccessorsV() const {
507 return getNumSuccessors();
508}
509void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
510 setSuccessor(idx, B);
511}
512
513
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000514//===----------------------------------------------------------------------===//
515// AllocationInst Implementation
516//===----------------------------------------------------------------------===//
517
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000518static Value *getAISize(Value *Amt) {
519 if (!Amt)
Reid Spencer8d9336d2006-12-31 05:26:44 +0000520 Amt = ConstantInt::get(Type::Int32Ty, 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000521 else {
522 assert(!isa<BasicBlock>(Amt) &&
523 "Passed basic block into allocation size parameter! Ue other ctor");
Reid Spencer8d9336d2006-12-31 05:26:44 +0000524 assert(Amt->getType() == Type::Int32Ty &&
Reid Spencer7e16e232007-01-26 06:30:34 +0000525 "Malloc/Allocation array size is not a 32-bit integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000526 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000527 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000528}
529
Misha Brukmanb1c93172005-04-21 23:48:37 +0000530AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000531 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000532 Instruction *InsertBefore)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000533 : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize),
Nate Begeman848622f2005-11-05 09:21:28 +0000534 Name, InsertBefore), Alignment(Align) {
Chris Lattner79b8c792005-11-05 21:57:54 +0000535 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000536 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000537}
538
Misha Brukmanb1c93172005-04-21 23:48:37 +0000539AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000540 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000541 BasicBlock *InsertAtEnd)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000542 : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize),
Nate Begeman848622f2005-11-05 09:21:28 +0000543 Name, InsertAtEnd), Alignment(Align) {
Chris Lattner79b8c792005-11-05 21:57:54 +0000544 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000545 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000546}
547
Chris Lattner1c12a882006-06-21 16:53:47 +0000548// Out of line virtual method, so the vtable, etc has a home.
549AllocationInst::~AllocationInst() {
550}
551
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000552bool AllocationInst::isArrayAllocation() const {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000553 if (ConstantInt *CUI = dyn_cast<ConstantInt>(getOperand(0)))
554 return CUI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000555 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000556}
557
558const Type *AllocationInst::getAllocatedType() const {
559 return getType()->getElementType();
560}
561
562AllocaInst::AllocaInst(const AllocaInst &AI)
563 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000564 Instruction::Alloca, AI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000565}
566
567MallocInst::MallocInst(const MallocInst &MI)
568 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000569 Instruction::Malloc, MI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000570}
571
572//===----------------------------------------------------------------------===//
573// FreeInst Implementation
574//===----------------------------------------------------------------------===//
575
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000576void FreeInst::AssertOK() {
577 assert(isa<PointerType>(getOperand(0)->getType()) &&
578 "Can not free something of nonpointer type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000579}
580
581FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000582 : UnaryInstruction(Type::VoidTy, Free, Ptr, "", InsertBefore) {
583 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000584}
585
586FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000587 : UnaryInstruction(Type::VoidTy, Free, Ptr, "", InsertAtEnd) {
588 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000589}
590
591
592//===----------------------------------------------------------------------===//
593// LoadInst Implementation
594//===----------------------------------------------------------------------===//
595
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000596void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000597 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000598 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000599}
600
601LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000602 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattnerdf57a022005-02-05 01:38:38 +0000603 Load, Ptr, Name, InsertBef) {
604 setVolatile(false);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000605 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000606}
607
608LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000609 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattnerdf57a022005-02-05 01:38:38 +0000610 Load, Ptr, Name, InsertAE) {
611 setVolatile(false);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000612 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000613}
614
615LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
616 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000617 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattnerdf57a022005-02-05 01:38:38 +0000618 Load, Ptr, Name, InsertBef) {
619 setVolatile(isVolatile);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000620 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000621}
622
623LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
624 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000625 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattnerdf57a022005-02-05 01:38:38 +0000626 Load, Ptr, Name, InsertAE) {
627 setVolatile(isVolatile);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000628 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000629}
630
631
632//===----------------------------------------------------------------------===//
633// StoreInst Implementation
634//===----------------------------------------------------------------------===//
635
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000636void StoreInst::AssertOK() {
637 assert(isa<PointerType>(getOperand(1)->getType()) &&
638 "Ptr must have pointer type!");
639 assert(getOperand(0)->getType() ==
640 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000641 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000642}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000643
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000644
645StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Chris Lattnerdf57a022005-02-05 01:38:38 +0000646 : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000647 Ops[0].init(val, this);
648 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000649 setVolatile(false);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000650 AssertOK();
651}
652
653StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Chris Lattnerdf57a022005-02-05 01:38:38 +0000654 : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000655 Ops[0].init(val, this);
656 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000657 setVolatile(false);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000658 AssertOK();
659}
660
Misha Brukmanb1c93172005-04-21 23:48:37 +0000661StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000662 Instruction *InsertBefore)
Chris Lattnerdf57a022005-02-05 01:38:38 +0000663 : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000664 Ops[0].init(val, this);
665 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000666 setVolatile(isVolatile);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000667 AssertOK();
668}
669
Misha Brukmanb1c93172005-04-21 23:48:37 +0000670StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000671 BasicBlock *InsertAtEnd)
Chris Lattnerdf57a022005-02-05 01:38:38 +0000672 : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000673 Ops[0].init(val, this);
674 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000675 setVolatile(isVolatile);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000676 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000677}
678
679//===----------------------------------------------------------------------===//
680// GetElementPtrInst Implementation
681//===----------------------------------------------------------------------===//
682
683// checkType - Simple wrapper function to give a better assertion failure
684// message on bad indexes for a gep instruction.
685//
686static inline const Type *checkType(const Type *Ty) {
Chris Lattner47a6e632006-05-14 18:34:36 +0000687 assert(Ty && "Invalid GetElementPtrInst indices for type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000688 return Ty;
689}
690
Chris Lattner79807c3d2007-01-31 19:47:18 +0000691void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx) {
692 NumOperands = 1+NumIdx;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000693 Use *OL = OperandList = new Use[NumOperands];
694 OL[0].init(Ptr, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000695
Chris Lattner79807c3d2007-01-31 19:47:18 +0000696 for (unsigned i = 0; i != NumIdx; ++i)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000697 OL[i+1].init(Idx[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000698}
699
700void GetElementPtrInst::init(Value *Ptr, Value *Idx0, Value *Idx1) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000701 NumOperands = 3;
702 Use *OL = OperandList = new Use[3];
703 OL[0].init(Ptr, this);
704 OL[1].init(Idx0, this);
705 OL[2].init(Idx1, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000706}
707
Chris Lattner82981202005-05-03 05:43:30 +0000708void GetElementPtrInst::init(Value *Ptr, Value *Idx) {
709 NumOperands = 2;
710 Use *OL = OperandList = new Use[2];
711 OL[0].init(Ptr, this);
712 OL[1].init(Idx, this);
713}
714
Chris Lattner79807c3d2007-01-31 19:47:18 +0000715
716GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value* const *Idx,
717 unsigned NumIdx,
718 const std::string &Name, Instruction *InBe)
719: Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
Reid Spencerdee14b52007-01-31 22:30:26 +0000720 Idx, NumIdx, true))),
Chris Lattner79807c3d2007-01-31 19:47:18 +0000721 GetElementPtr, 0, 0, Name, InBe) {
722 init(Ptr, Idx, NumIdx);
723}
724
725GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value* const *Idx,
726 unsigned NumIdx,
727 const std::string &Name, BasicBlock *IAE)
728: Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
Reid Spencerdee14b52007-01-31 22:30:26 +0000729 Idx, NumIdx, true))),
Chris Lattner79807c3d2007-01-31 19:47:18 +0000730 GetElementPtr, 0, 0, Name, IAE) {
731 init(Ptr, Idx, NumIdx);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000732}
733
Chris Lattner82981202005-05-03 05:43:30 +0000734GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
735 const std::string &Name, Instruction *InBe)
Reid Spencerdee14b52007-01-31 22:30:26 +0000736 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
737 Idx))),
Chris Lattner82981202005-05-03 05:43:30 +0000738 GetElementPtr, 0, 0, Name, InBe) {
739 init(Ptr, Idx);
740}
741
742GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
743 const std::string &Name, BasicBlock *IAE)
Reid Spencerdee14b52007-01-31 22:30:26 +0000744 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
745 Idx))),
Chris Lattner82981202005-05-03 05:43:30 +0000746 GetElementPtr, 0, 0, Name, IAE) {
747 init(Ptr, Idx);
748}
749
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000750GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
751 const std::string &Name, Instruction *InBe)
752 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
753 Idx0, Idx1, true))),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000754 GetElementPtr, 0, 0, Name, InBe) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000755 init(Ptr, Idx0, Idx1);
756}
757
758GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
Misha Brukman96eb8782005-03-16 05:42:00 +0000759 const std::string &Name, BasicBlock *IAE)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000760 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
761 Idx0, Idx1, true))),
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000762 GetElementPtr, 0, 0, Name, IAE) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000763 init(Ptr, Idx0, Idx1);
764}
765
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000766GetElementPtrInst::~GetElementPtrInst() {
767 delete[] OperandList;
768}
769
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000770// getIndexedType - Returns the type of the element that would be loaded with
771// a load instruction with the specified parameters.
772//
Misha Brukmanb1c93172005-04-21 23:48:37 +0000773// A null type is returned if the indices are invalid for the specified
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000774// pointer type.
775//
Misha Brukmanb1c93172005-04-21 23:48:37 +0000776const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Chris Lattner302116a2007-01-31 04:40:28 +0000777 Value* const *Idxs,
778 unsigned NumIdx,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000779 bool AllowCompositeLeaf) {
780 if (!isa<PointerType>(Ptr)) return 0; // Type isn't a pointer type!
781
782 // Handle the special case of the empty set index set...
Chris Lattner302116a2007-01-31 04:40:28 +0000783 if (NumIdx == 0)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000784 if (AllowCompositeLeaf ||
785 cast<PointerType>(Ptr)->getElementType()->isFirstClassType())
786 return cast<PointerType>(Ptr)->getElementType();
787 else
788 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000789
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000790 unsigned CurIdx = 0;
791 while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
Chris Lattner302116a2007-01-31 04:40:28 +0000792 if (NumIdx == CurIdx) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000793 if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr;
794 return 0; // Can't load a whole structure or array!?!?
795 }
796
Chris Lattner302116a2007-01-31 04:40:28 +0000797 Value *Index = Idxs[CurIdx++];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000798 if (isa<PointerType>(CT) && CurIdx != 1)
799 return 0; // Can only index into pointer types at the first index!
800 if (!CT->indexValid(Index)) return 0;
801 Ptr = CT->getTypeAtIndex(Index);
802
803 // If the new type forwards to another type, then it is in the middle
804 // of being refined to another type (and hence, may have dropped all
805 // references to what it was using before). So, use the new forwarded
806 // type.
807 if (const Type * Ty = Ptr->getForwardedType()) {
808 Ptr = Ty;
809 }
810 }
Chris Lattner302116a2007-01-31 04:40:28 +0000811 return CurIdx == NumIdx ? Ptr : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000812}
813
Misha Brukmanb1c93172005-04-21 23:48:37 +0000814const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000815 Value *Idx0, Value *Idx1,
816 bool AllowCompositeLeaf) {
817 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
818 if (!PTy) return 0; // Type isn't a pointer type!
819
820 // Check the pointer index.
821 if (!PTy->indexValid(Idx0)) return 0;
822
823 const CompositeType *CT = dyn_cast<CompositeType>(PTy->getElementType());
824 if (!CT || !CT->indexValid(Idx1)) return 0;
825
826 const Type *ElTy = CT->getTypeAtIndex(Idx1);
827 if (AllowCompositeLeaf || ElTy->isFirstClassType())
828 return ElTy;
829 return 0;
830}
831
Chris Lattner82981202005-05-03 05:43:30 +0000832const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
833 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
834 if (!PTy) return 0; // Type isn't a pointer type!
835
836 // Check the pointer index.
837 if (!PTy->indexValid(Idx)) return 0;
838
Chris Lattnerc2233332005-05-03 16:44:45 +0000839 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +0000840}
841
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000842//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +0000843// ExtractElementInst Implementation
844//===----------------------------------------------------------------------===//
845
846ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000847 const std::string &Name,
848 Instruction *InsertBef)
Robert Bocchino23004482006-01-10 19:05:34 +0000849 : Instruction(cast<PackedType>(Val->getType())->getElementType(),
850 ExtractElement, Ops, 2, Name, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +0000851 assert(isValidOperands(Val, Index) &&
852 "Invalid extractelement instruction operands!");
Robert Bocchino23004482006-01-10 19:05:34 +0000853 Ops[0].init(Val, this);
854 Ops[1].init(Index, this);
855}
856
Chris Lattner65511ff2006-10-05 06:24:58 +0000857ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
858 const std::string &Name,
859 Instruction *InsertBef)
860 : Instruction(cast<PackedType>(Val->getType())->getElementType(),
861 ExtractElement, Ops, 2, Name, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +0000862 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +0000863 assert(isValidOperands(Val, Index) &&
864 "Invalid extractelement instruction operands!");
865 Ops[0].init(Val, this);
866 Ops[1].init(Index, this);
867}
868
869
Robert Bocchino23004482006-01-10 19:05:34 +0000870ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000871 const std::string &Name,
872 BasicBlock *InsertAE)
Robert Bocchino23004482006-01-10 19:05:34 +0000873 : Instruction(cast<PackedType>(Val->getType())->getElementType(),
874 ExtractElement, Ops, 2, Name, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +0000875 assert(isValidOperands(Val, Index) &&
876 "Invalid extractelement instruction operands!");
877
Robert Bocchino23004482006-01-10 19:05:34 +0000878 Ops[0].init(Val, this);
879 Ops[1].init(Index, this);
880}
881
Chris Lattner65511ff2006-10-05 06:24:58 +0000882ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
883 const std::string &Name,
884 BasicBlock *InsertAE)
885 : Instruction(cast<PackedType>(Val->getType())->getElementType(),
886 ExtractElement, Ops, 2, Name, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +0000887 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +0000888 assert(isValidOperands(Val, Index) &&
889 "Invalid extractelement instruction operands!");
890
891 Ops[0].init(Val, this);
892 Ops[1].init(Index, this);
893}
894
895
Chris Lattner54865b32006-04-08 04:05:48 +0000896bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Reid Spencer8d9336d2006-12-31 05:26:44 +0000897 if (!isa<PackedType>(Val->getType()) || Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +0000898 return false;
899 return true;
900}
901
902
Robert Bocchino23004482006-01-10 19:05:34 +0000903//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +0000904// InsertElementInst Implementation
905//===----------------------------------------------------------------------===//
906
Chris Lattner0875d942006-04-14 22:20:32 +0000907InsertElementInst::InsertElementInst(const InsertElementInst &IE)
908 : Instruction(IE.getType(), InsertElement, Ops, 3) {
909 Ops[0].init(IE.Ops[0], this);
910 Ops[1].init(IE.Ops[1], this);
911 Ops[2].init(IE.Ops[2], this);
912}
Chris Lattner54865b32006-04-08 04:05:48 +0000913InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000914 const std::string &Name,
915 Instruction *InsertBef)
Chris Lattner54865b32006-04-08 04:05:48 +0000916 : Instruction(Vec->getType(), InsertElement, Ops, 3, Name, InsertBef) {
917 assert(isValidOperands(Vec, Elt, Index) &&
918 "Invalid insertelement instruction operands!");
919 Ops[0].init(Vec, this);
Robert Bocchinoca27f032006-01-17 20:07:22 +0000920 Ops[1].init(Elt, this);
921 Ops[2].init(Index, this);
922}
923
Chris Lattner65511ff2006-10-05 06:24:58 +0000924InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
925 const std::string &Name,
926 Instruction *InsertBef)
927 : Instruction(Vec->getType(), InsertElement, Ops, 3, Name, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +0000928 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +0000929 assert(isValidOperands(Vec, Elt, Index) &&
930 "Invalid insertelement instruction operands!");
931 Ops[0].init(Vec, this);
932 Ops[1].init(Elt, this);
933 Ops[2].init(Index, this);
934}
935
936
Chris Lattner54865b32006-04-08 04:05:48 +0000937InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000938 const std::string &Name,
939 BasicBlock *InsertAE)
Chris Lattner54865b32006-04-08 04:05:48 +0000940 : Instruction(Vec->getType(), InsertElement, Ops, 3, Name, InsertAE) {
941 assert(isValidOperands(Vec, Elt, Index) &&
942 "Invalid insertelement instruction operands!");
943
944 Ops[0].init(Vec, this);
Robert Bocchinoca27f032006-01-17 20:07:22 +0000945 Ops[1].init(Elt, this);
946 Ops[2].init(Index, this);
947}
948
Chris Lattner65511ff2006-10-05 06:24:58 +0000949InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
950 const std::string &Name,
951 BasicBlock *InsertAE)
952: Instruction(Vec->getType(), InsertElement, Ops, 3, Name, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +0000953 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +0000954 assert(isValidOperands(Vec, Elt, Index) &&
955 "Invalid insertelement instruction operands!");
956
957 Ops[0].init(Vec, this);
958 Ops[1].init(Elt, this);
959 Ops[2].init(Index, this);
960}
961
Chris Lattner54865b32006-04-08 04:05:48 +0000962bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
963 const Value *Index) {
964 if (!isa<PackedType>(Vec->getType()))
965 return false; // First operand of insertelement must be packed type.
966
967 if (Elt->getType() != cast<PackedType>(Vec->getType())->getElementType())
968 return false;// Second operand of insertelement must be packed element type.
969
Reid Spencer8d9336d2006-12-31 05:26:44 +0000970 if (Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +0000971 return false; // Third operand of insertelement must be uint.
972 return true;
973}
974
975
Robert Bocchinoca27f032006-01-17 20:07:22 +0000976//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000977// ShuffleVectorInst Implementation
978//===----------------------------------------------------------------------===//
979
Chris Lattner0875d942006-04-14 22:20:32 +0000980ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV)
981 : Instruction(SV.getType(), ShuffleVector, Ops, 3) {
982 Ops[0].init(SV.Ops[0], this);
983 Ops[1].init(SV.Ops[1], this);
984 Ops[2].init(SV.Ops[2], this);
985}
986
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000987ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
988 const std::string &Name,
989 Instruction *InsertBefore)
990 : Instruction(V1->getType(), ShuffleVector, Ops, 3, Name, InsertBefore) {
991 assert(isValidOperands(V1, V2, Mask) &&
992 "Invalid shuffle vector instruction operands!");
993 Ops[0].init(V1, this);
994 Ops[1].init(V2, this);
995 Ops[2].init(Mask, this);
996}
997
998ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
999 const std::string &Name,
1000 BasicBlock *InsertAtEnd)
1001 : Instruction(V1->getType(), ShuffleVector, Ops, 3, Name, InsertAtEnd) {
1002 assert(isValidOperands(V1, V2, Mask) &&
1003 "Invalid shuffle vector instruction operands!");
1004
1005 Ops[0].init(V1, this);
1006 Ops[1].init(V2, this);
1007 Ops[2].init(Mask, this);
1008}
1009
1010bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
1011 const Value *Mask) {
1012 if (!isa<PackedType>(V1->getType())) return false;
1013 if (V1->getType() != V2->getType()) return false;
1014 if (!isa<PackedType>(Mask->getType()) ||
Reid Spencer8d9336d2006-12-31 05:26:44 +00001015 cast<PackedType>(Mask->getType())->getElementType() != Type::Int32Ty ||
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001016 cast<PackedType>(Mask->getType())->getNumElements() !=
1017 cast<PackedType>(V1->getType())->getNumElements())
1018 return false;
1019 return true;
1020}
1021
1022
1023//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001024// BinaryOperator Class
1025//===----------------------------------------------------------------------===//
1026
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001027void BinaryOperator::init(BinaryOps iType)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001028{
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001029 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001030 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001031 assert(LHS->getType() == RHS->getType() &&
1032 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001033#ifndef NDEBUG
1034 switch (iType) {
1035 case Add: case Sub:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001036 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001037 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001038 "Arithmetic operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001039 assert((getType()->isInteger() || getType()->isFloatingPoint() ||
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001040 isa<PackedType>(getType())) &&
Brian Gaeke02209042004-08-20 06:00:58 +00001041 "Tried to create an arithmetic operation on a non-arithmetic type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001042 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001043 case UDiv:
1044 case SDiv:
1045 assert(getType() == LHS->getType() &&
1046 "Arithmetic operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001047 assert((getType()->isInteger() || (isa<PackedType>(getType()) &&
1048 cast<PackedType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001049 "Incorrect operand type (not integer) for S/UDIV");
1050 break;
1051 case FDiv:
1052 assert(getType() == LHS->getType() &&
1053 "Arithmetic operation should return same type as operands!");
1054 assert((getType()->isFloatingPoint() || (isa<PackedType>(getType()) &&
1055 cast<PackedType>(getType())->getElementType()->isFloatingPoint()))
1056 && "Incorrect operand type (not floating point) for FDIV");
1057 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001058 case URem:
1059 case SRem:
1060 assert(getType() == LHS->getType() &&
1061 "Arithmetic operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001062 assert((getType()->isInteger() || (isa<PackedType>(getType()) &&
1063 cast<PackedType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001064 "Incorrect operand type (not integer) for S/UREM");
1065 break;
1066 case FRem:
1067 assert(getType() == LHS->getType() &&
1068 "Arithmetic operation should return same type as operands!");
1069 assert((getType()->isFloatingPoint() || (isa<PackedType>(getType()) &&
1070 cast<PackedType>(getType())->getElementType()->isFloatingPoint()))
1071 && "Incorrect operand type (not floating point) for FREM");
1072 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001073 case Shl:
1074 case LShr:
1075 case AShr:
1076 assert(getType() == LHS->getType() &&
1077 "Shift operation should return same type as operands!");
1078 assert(getType()->isInteger() &&
1079 "Shift operation requires integer operands");
1080 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001081 case And: case Or:
1082 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001083 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001084 "Logical operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001085 assert((getType()->isInteger() ||
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001086 (isa<PackedType>(getType()) &&
Chris Lattner03c49532007-01-15 02:27:26 +00001087 cast<PackedType>(getType())->getElementType()->isInteger())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001088 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001089 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001090 default:
1091 break;
1092 }
1093#endif
1094}
1095
1096BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001097 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001098 Instruction *InsertBefore) {
1099 assert(S1->getType() == S2->getType() &&
1100 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001101 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001102}
1103
1104BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001105 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001106 BasicBlock *InsertAtEnd) {
1107 BinaryOperator *Res = create(Op, S1, S2, Name);
1108 InsertAtEnd->getInstList().push_back(Res);
1109 return Res;
1110}
1111
1112BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1113 Instruction *InsertBefore) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001114 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1115 return new BinaryOperator(Instruction::Sub,
1116 zero, Op,
1117 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001118}
1119
1120BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1121 BasicBlock *InsertAtEnd) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001122 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1123 return new BinaryOperator(Instruction::Sub,
1124 zero, Op,
1125 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001126}
1127
1128BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1129 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001130 Constant *C;
1131 if (const PackedType *PTy = dyn_cast<PackedType>(Op->getType())) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001132 C = ConstantInt::getAllOnesValue(PTy->getElementType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001133 C = ConstantPacked::get(std::vector<Constant*>(PTy->getNumElements(), C));
1134 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001135 C = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001136 }
1137
1138 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001139 Op->getType(), Name, InsertBefore);
1140}
1141
1142BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1143 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001144 Constant *AllOnes;
1145 if (const PackedType *PTy = dyn_cast<PackedType>(Op->getType())) {
1146 // Create a vector of all ones values.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001147 Constant *Elt = ConstantInt::getAllOnesValue(PTy->getElementType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001148 AllOnes =
1149 ConstantPacked::get(std::vector<Constant*>(PTy->getNumElements(), Elt));
1150 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001151 AllOnes = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001152 }
1153
1154 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001155 Op->getType(), Name, InsertAtEnd);
1156}
1157
1158
1159// isConstantAllOnes - Helper function for several functions below
1160static inline bool isConstantAllOnes(const Value *V) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001161 return isa<ConstantInt>(V) &&cast<ConstantInt>(V)->isAllOnesValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001162}
1163
1164bool BinaryOperator::isNeg(const Value *V) {
1165 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1166 if (Bop->getOpcode() == Instruction::Sub)
Reid Spencer2eadb532007-01-21 00:29:26 +00001167 return Bop->getOperand(0) ==
1168 ConstantExpr::getZeroValueForNegationExpr(Bop->getType());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001169 return false;
1170}
1171
1172bool BinaryOperator::isNot(const Value *V) {
1173 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1174 return (Bop->getOpcode() == Instruction::Xor &&
1175 (isConstantAllOnes(Bop->getOperand(1)) ||
1176 isConstantAllOnes(Bop->getOperand(0))));
1177 return false;
1178}
1179
Chris Lattner2c7d1772005-04-24 07:28:37 +00001180Value *BinaryOperator::getNegArgument(Value *BinOp) {
1181 assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!");
1182 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001183}
1184
Chris Lattner2c7d1772005-04-24 07:28:37 +00001185const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1186 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001187}
1188
Chris Lattner2c7d1772005-04-24 07:28:37 +00001189Value *BinaryOperator::getNotArgument(Value *BinOp) {
1190 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1191 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1192 Value *Op0 = BO->getOperand(0);
1193 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001194 if (isConstantAllOnes(Op0)) return Op1;
1195
1196 assert(isConstantAllOnes(Op1));
1197 return Op0;
1198}
1199
Chris Lattner2c7d1772005-04-24 07:28:37 +00001200const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1201 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001202}
1203
1204
1205// swapOperands - Exchange the two operands to this instruction. This
1206// instruction is safe to use on any binary instruction and does not
1207// modify the semantics of the instruction. If the instruction is
1208// order dependent (SetLT f.e.) the opcode is changed.
1209//
1210bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001211 if (!isCommutative())
1212 return true; // Can't commute operands
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001213 std::swap(Ops[0], Ops[1]);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001214 return false;
1215}
1216
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001217//===----------------------------------------------------------------------===//
1218// CastInst Class
1219//===----------------------------------------------------------------------===//
1220
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001221// Just determine if this cast only deals with integral->integral conversion.
1222bool CastInst::isIntegerCast() const {
1223 switch (getOpcode()) {
1224 default: return false;
1225 case Instruction::ZExt:
1226 case Instruction::SExt:
1227 case Instruction::Trunc:
1228 return true;
1229 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001230 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001231 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001232}
1233
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001234bool CastInst::isLosslessCast() const {
1235 // Only BitCast can be lossless, exit fast if we're not BitCast
1236 if (getOpcode() != Instruction::BitCast)
1237 return false;
1238
1239 // Identity cast is always lossless
1240 const Type* SrcTy = getOperand(0)->getType();
1241 const Type* DstTy = getType();
1242 if (SrcTy == DstTy)
1243 return true;
1244
Reid Spencer8d9336d2006-12-31 05:26:44 +00001245 // Pointer to pointer is always lossless.
1246 if (isa<PointerType>(SrcTy))
1247 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001248 return false; // Other types have no identity values
1249}
1250
1251/// This function determines if the CastInst does not require any bits to be
1252/// changed in order to effect the cast. Essentially, it identifies cases where
1253/// no code gen is necessary for the cast, hence the name no-op cast. For
1254/// example, the following are all no-op casts:
1255/// # bitcast uint %X, int
1256/// # bitcast uint* %x, sbyte*
1257/// # bitcast packed< 2 x int > %x, packed< 4 x short>
1258/// # ptrtoint uint* %x, uint ; on 32-bit plaforms only
1259/// @brief Determine if a cast is a no-op.
1260bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1261 switch (getOpcode()) {
1262 default:
1263 assert(!"Invalid CastOp");
1264 case Instruction::Trunc:
1265 case Instruction::ZExt:
1266 case Instruction::SExt:
1267 case Instruction::FPTrunc:
1268 case Instruction::FPExt:
1269 case Instruction::UIToFP:
1270 case Instruction::SIToFP:
1271 case Instruction::FPToUI:
1272 case Instruction::FPToSI:
1273 return false; // These always modify bits
1274 case Instruction::BitCast:
1275 return true; // BitCast never modifies bits.
1276 case Instruction::PtrToInt:
1277 return IntPtrTy->getPrimitiveSizeInBits() ==
1278 getType()->getPrimitiveSizeInBits();
1279 case Instruction::IntToPtr:
1280 return IntPtrTy->getPrimitiveSizeInBits() ==
1281 getOperand(0)->getType()->getPrimitiveSizeInBits();
1282 }
1283}
1284
1285/// This function determines if a pair of casts can be eliminated and what
1286/// opcode should be used in the elimination. This assumes that there are two
1287/// instructions like this:
1288/// * %F = firstOpcode SrcTy %x to MidTy
1289/// * %S = secondOpcode MidTy %F to DstTy
1290/// The function returns a resultOpcode so these two casts can be replaced with:
1291/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1292/// If no such cast is permited, the function returns 0.
1293unsigned CastInst::isEliminableCastPair(
1294 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1295 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1296{
1297 // Define the 144 possibilities for these two cast instructions. The values
1298 // in this matrix determine what to do in a given situation and select the
1299 // case in the switch below. The rows correspond to firstOp, the columns
1300 // correspond to secondOp. In looking at the table below, keep in mind
1301 // the following cast properties:
1302 //
1303 // Size Compare Source Destination
1304 // Operator Src ? Size Type Sign Type Sign
1305 // -------- ------------ ------------------- ---------------------
1306 // TRUNC > Integer Any Integral Any
1307 // ZEXT < Integral Unsigned Integer Any
1308 // SEXT < Integral Signed Integer Any
1309 // FPTOUI n/a FloatPt n/a Integral Unsigned
1310 // FPTOSI n/a FloatPt n/a Integral Signed
1311 // UITOFP n/a Integral Unsigned FloatPt n/a
1312 // SITOFP n/a Integral Signed FloatPt n/a
1313 // FPTRUNC > FloatPt n/a FloatPt n/a
1314 // FPEXT < FloatPt n/a FloatPt n/a
1315 // PTRTOINT n/a Pointer n/a Integral Unsigned
1316 // INTTOPTR n/a Integral Unsigned Pointer n/a
1317 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001318 //
1319 // NOTE: some transforms are safe, but we consider them to be non-profitable.
1320 // For example, we could merge "fptoui double to uint" + "zext uint to ulong",
1321 // into "fptoui double to ulong", but this loses information about the range
1322 // of the produced value (we no longer know the top-part is all zeros).
1323 // Further this conversion is often much more expensive for typical hardware,
1324 // and causes issues when building libgcc. We disallow fptosi+sext for the
1325 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001326 const unsigned numCastOps =
1327 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1328 static const uint8_t CastResults[numCastOps][numCastOps] = {
1329 // T F F U S F F P I B -+
1330 // R Z S P P I I T P 2 N T |
1331 // U E E 2 2 2 2 R E I T C +- secondOp
1332 // N X X U S F F N X N 2 V |
1333 // C T T I I P P C T T P T -+
1334 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1335 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1336 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001337 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1338 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001339 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1340 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1341 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1342 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1343 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1344 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1345 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1346 };
1347
1348 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1349 [secondOp-Instruction::CastOpsBegin];
1350 switch (ElimCase) {
1351 case 0:
1352 // categorically disallowed
1353 return 0;
1354 case 1:
1355 // allowed, use first cast's opcode
1356 return firstOp;
1357 case 2:
1358 // allowed, use second cast's opcode
1359 return secondOp;
1360 case 3:
1361 // no-op cast in second op implies firstOp as long as the DestTy
1362 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00001363 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001364 return firstOp;
1365 return 0;
1366 case 4:
1367 // no-op cast in second op implies firstOp as long as the DestTy
1368 // is floating point
1369 if (DstTy->isFloatingPoint())
1370 return firstOp;
1371 return 0;
1372 case 5:
1373 // no-op cast in first op implies secondOp as long as the SrcTy
1374 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00001375 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001376 return secondOp;
1377 return 0;
1378 case 6:
1379 // no-op cast in first op implies secondOp as long as the SrcTy
1380 // is a floating point
1381 if (SrcTy->isFloatingPoint())
1382 return secondOp;
1383 return 0;
1384 case 7: {
1385 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
1386 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1387 unsigned MidSize = MidTy->getPrimitiveSizeInBits();
1388 if (MidSize >= PtrSize)
1389 return Instruction::BitCast;
1390 return 0;
1391 }
1392 case 8: {
1393 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
1394 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
1395 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
1396 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1397 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1398 if (SrcSize == DstSize)
1399 return Instruction::BitCast;
1400 else if (SrcSize < DstSize)
1401 return firstOp;
1402 return secondOp;
1403 }
1404 case 9: // zext, sext -> zext, because sext can't sign extend after zext
1405 return Instruction::ZExt;
1406 case 10:
1407 // fpext followed by ftrunc is allowed if the bit size returned to is
1408 // the same as the original, in which case its just a bitcast
1409 if (SrcTy == DstTy)
1410 return Instruction::BitCast;
1411 return 0; // If the types are not the same we can't eliminate it.
1412 case 11:
1413 // bitcast followed by ptrtoint is allowed as long as the bitcast
1414 // is a pointer to pointer cast.
1415 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
1416 return secondOp;
1417 return 0;
1418 case 12:
1419 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
1420 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
1421 return firstOp;
1422 return 0;
1423 case 13: {
1424 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
1425 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1426 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1427 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1428 if (SrcSize <= PtrSize && SrcSize == DstSize)
1429 return Instruction::BitCast;
1430 return 0;
1431 }
1432 case 99:
1433 // cast combination can't happen (error in input). This is for all cases
1434 // where the MidTy is not the same for the two cast instructions.
1435 assert(!"Invalid Cast Combination");
1436 return 0;
1437 default:
1438 assert(!"Error in CastResults table!!!");
1439 return 0;
1440 }
1441 return 0;
1442}
1443
1444CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
1445 const std::string &Name, Instruction *InsertBefore) {
1446 // Construct and return the appropriate CastInst subclass
1447 switch (op) {
1448 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
1449 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
1450 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
1451 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
1452 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
1453 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
1454 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
1455 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
1456 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
1457 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
1458 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
1459 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
1460 default:
1461 assert(!"Invalid opcode provided");
1462 }
1463 return 0;
1464}
1465
1466CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
1467 const std::string &Name, BasicBlock *InsertAtEnd) {
1468 // Construct and return the appropriate CastInst subclass
1469 switch (op) {
1470 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
1471 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
1472 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
1473 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
1474 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
1475 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
1476 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
1477 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
1478 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
1479 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
1480 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
1481 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
1482 default:
1483 assert(!"Invalid opcode provided");
1484 }
1485 return 0;
1486}
1487
Reid Spencer5c140882006-12-04 20:17:56 +00001488CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
1489 const std::string &Name,
1490 Instruction *InsertBefore) {
1491 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1492 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1493 return create(Instruction::ZExt, S, Ty, Name, InsertBefore);
1494}
1495
1496CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
1497 const std::string &Name,
1498 BasicBlock *InsertAtEnd) {
1499 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1500 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1501 return create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
1502}
1503
1504CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
1505 const std::string &Name,
1506 Instruction *InsertBefore) {
1507 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1508 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1509 return create(Instruction::SExt, S, Ty, Name, InsertBefore);
1510}
1511
1512CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
1513 const std::string &Name,
1514 BasicBlock *InsertAtEnd) {
1515 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1516 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1517 return create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
1518}
1519
1520CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
1521 const std::string &Name,
1522 Instruction *InsertBefore) {
1523 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1524 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1525 return create(Instruction::Trunc, S, Ty, Name, InsertBefore);
1526}
1527
1528CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
1529 const std::string &Name,
1530 BasicBlock *InsertAtEnd) {
1531 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1532 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1533 return create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
1534}
1535
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001536CastInst *CastInst::createPointerCast(Value *S, const Type *Ty,
1537 const std::string &Name,
1538 BasicBlock *InsertAtEnd) {
1539 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001540 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001541 "Invalid cast");
1542
Chris Lattner03c49532007-01-15 02:27:26 +00001543 if (Ty->isInteger())
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001544 return create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
1545 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1546}
1547
1548/// @brief Create a BitCast or a PtrToInt cast instruction
1549CastInst *CastInst::createPointerCast(Value *S, const Type *Ty,
1550 const std::string &Name,
1551 Instruction *InsertBefore) {
1552 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001553 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001554 "Invalid cast");
1555
Chris Lattner03c49532007-01-15 02:27:26 +00001556 if (Ty->isInteger())
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001557 return create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
1558 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1559}
1560
Reid Spencer7e933472006-12-12 00:49:44 +00001561CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty,
1562 bool isSigned, const std::string &Name,
1563 Instruction *InsertBefore) {
Chris Lattner03c49532007-01-15 02:27:26 +00001564 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001565 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1566 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1567 Instruction::CastOps opcode =
1568 (SrcBits == DstBits ? Instruction::BitCast :
1569 (SrcBits > DstBits ? Instruction::Trunc :
1570 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1571 return create(opcode, C, Ty, Name, InsertBefore);
1572}
1573
1574CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty,
1575 bool isSigned, const std::string &Name,
1576 BasicBlock *InsertAtEnd) {
Chris Lattner03c49532007-01-15 02:27:26 +00001577 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001578 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1579 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1580 Instruction::CastOps opcode =
1581 (SrcBits == DstBits ? Instruction::BitCast :
1582 (SrcBits > DstBits ? Instruction::Trunc :
1583 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1584 return create(opcode, C, Ty, Name, InsertAtEnd);
1585}
1586
1587CastInst *CastInst::createFPCast(Value *C, const Type *Ty,
1588 const std::string &Name,
1589 Instruction *InsertBefore) {
1590 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1591 "Invalid cast");
1592 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1593 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1594 Instruction::CastOps opcode =
1595 (SrcBits == DstBits ? Instruction::BitCast :
1596 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
1597 return create(opcode, C, Ty, Name, InsertBefore);
1598}
1599
1600CastInst *CastInst::createFPCast(Value *C, const Type *Ty,
1601 const std::string &Name,
1602 BasicBlock *InsertAtEnd) {
1603 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1604 "Invalid cast");
1605 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1606 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1607 Instruction::CastOps opcode =
1608 (SrcBits == DstBits ? Instruction::BitCast :
1609 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
1610 return create(opcode, C, Ty, Name, InsertAtEnd);
1611}
1612
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001613// Provide a way to get a "cast" where the cast opcode is inferred from the
1614// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001615// logic in the castIsValid function below. This axiom should hold:
1616// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
1617// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001618// casting opcode for the arguments passed to it.
1619Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00001620CastInst::getCastOpcode(
1621 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001622 // Get the bit sizes, we'll need these
1623 const Type *SrcTy = Src->getType();
1624 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/packed
1625 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/packed
1626
1627 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00001628 if (DestTy->isInteger()) { // Casting to integral
1629 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001630 if (DestBits < SrcBits)
1631 return Trunc; // int -> smaller int
1632 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00001633 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001634 return SExt; // signed -> SEXT
1635 else
1636 return ZExt; // unsigned -> ZEXT
1637 } else {
1638 return BitCast; // Same size, No-op cast
1639 }
1640 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00001641 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001642 return FPToSI; // FP -> sint
1643 else
1644 return FPToUI; // FP -> uint
1645 } else if (const PackedType *PTy = dyn_cast<PackedType>(SrcTy)) {
1646 assert(DestBits == PTy->getBitWidth() &&
1647 "Casting packed to integer of different width");
1648 return BitCast; // Same size, no-op cast
1649 } else {
1650 assert(isa<PointerType>(SrcTy) &&
1651 "Casting from a value that is not first-class type");
1652 return PtrToInt; // ptr -> int
1653 }
1654 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00001655 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00001656 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001657 return SIToFP; // sint -> FP
1658 else
1659 return UIToFP; // uint -> FP
1660 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
1661 if (DestBits < SrcBits) {
1662 return FPTrunc; // FP -> smaller FP
1663 } else if (DestBits > SrcBits) {
1664 return FPExt; // FP -> larger FP
1665 } else {
1666 return BitCast; // same size, no-op cast
1667 }
1668 } else if (const PackedType *PTy = dyn_cast<PackedType>(SrcTy)) {
1669 assert(DestBits == PTy->getBitWidth() &&
1670 "Casting packed to floating point of different width");
1671 return BitCast; // same size, no-op cast
1672 } else {
1673 assert(0 && "Casting pointer or non-first class to float");
1674 }
1675 } else if (const PackedType *DestPTy = dyn_cast<PackedType>(DestTy)) {
1676 if (const PackedType *SrcPTy = dyn_cast<PackedType>(SrcTy)) {
1677 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
1678 "Casting packed to packed of different widths");
1679 return BitCast; // packed -> packed
1680 } else if (DestPTy->getBitWidth() == SrcBits) {
1681 return BitCast; // float/int -> packed
1682 } else {
1683 assert(!"Illegal cast to packed (wrong type or size)");
1684 }
1685 } else if (isa<PointerType>(DestTy)) {
1686 if (isa<PointerType>(SrcTy)) {
1687 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00001688 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001689 return IntToPtr; // int -> ptr
1690 } else {
1691 assert(!"Casting pointer to other than pointer or int");
1692 }
1693 } else {
1694 assert(!"Casting to type that is not first-class");
1695 }
1696
1697 // If we fall through to here we probably hit an assertion cast above
1698 // and assertions are not turned on. Anything we return is an error, so
1699 // BitCast is as good a choice as any.
1700 return BitCast;
1701}
1702
1703//===----------------------------------------------------------------------===//
1704// CastInst SubClass Constructors
1705//===----------------------------------------------------------------------===//
1706
1707/// Check that the construction parameters for a CastInst are correct. This
1708/// could be broken out into the separate constructors but it is useful to have
1709/// it in one place and to eliminate the redundant code for getting the sizes
1710/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001711bool
1712CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001713
1714 // Check for type sanity on the arguments
1715 const Type *SrcTy = S->getType();
1716 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
1717 return false;
1718
1719 // Get the size of the types in bits, we'll need this later
1720 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
1721 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
1722
1723 // Switch on the opcode provided
1724 switch (op) {
1725 default: return false; // This is an input error
1726 case Instruction::Trunc:
Chris Lattner03c49532007-01-15 02:27:26 +00001727 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001728 case Instruction::ZExt:
Chris Lattner03c49532007-01-15 02:27:26 +00001729 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001730 case Instruction::SExt:
Chris Lattner03c49532007-01-15 02:27:26 +00001731 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001732 case Instruction::FPTrunc:
1733 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
1734 SrcBitSize > DstBitSize;
1735 case Instruction::FPExt:
1736 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
1737 SrcBitSize < DstBitSize;
1738 case Instruction::UIToFP:
Chris Lattner03c49532007-01-15 02:27:26 +00001739 return SrcTy->isInteger() && DstTy->isFloatingPoint();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001740 case Instruction::SIToFP:
Chris Lattner03c49532007-01-15 02:27:26 +00001741 return SrcTy->isInteger() && DstTy->isFloatingPoint();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001742 case Instruction::FPToUI:
Chris Lattner03c49532007-01-15 02:27:26 +00001743 return SrcTy->isFloatingPoint() && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001744 case Instruction::FPToSI:
Chris Lattner03c49532007-01-15 02:27:26 +00001745 return SrcTy->isFloatingPoint() && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001746 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00001747 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001748 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00001749 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001750 case Instruction::BitCast:
1751 // BitCast implies a no-op cast of type only. No bits change.
1752 // However, you can't cast pointers to anything but pointers.
1753 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
1754 return false;
1755
1756 // Now we know we're not dealing with a pointer/non-poiner mismatch. In all
1757 // these cases, the cast is okay if the source and destination bit widths
1758 // are identical.
1759 return SrcBitSize == DstBitSize;
1760 }
1761}
1762
1763TruncInst::TruncInst(
1764 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1765) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001766 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001767}
1768
1769TruncInst::TruncInst(
1770 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1771) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001772 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001773}
1774
1775ZExtInst::ZExtInst(
1776 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1777) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001778 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001779}
1780
1781ZExtInst::ZExtInst(
1782 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1783) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001784 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001785}
1786SExtInst::SExtInst(
1787 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1788) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001789 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001790}
1791
Jeff Cohencc08c832006-12-02 02:22:01 +00001792SExtInst::SExtInst(
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001793 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1794) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001795 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001796}
1797
1798FPTruncInst::FPTruncInst(
1799 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1800) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001801 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001802}
1803
1804FPTruncInst::FPTruncInst(
1805 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1806) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001807 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001808}
1809
1810FPExtInst::FPExtInst(
1811 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1812) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001813 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001814}
1815
1816FPExtInst::FPExtInst(
1817 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1818) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001819 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001820}
1821
1822UIToFPInst::UIToFPInst(
1823 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1824) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001825 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001826}
1827
1828UIToFPInst::UIToFPInst(
1829 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1830) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001831 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001832}
1833
1834SIToFPInst::SIToFPInst(
1835 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1836) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001837 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001838}
1839
1840SIToFPInst::SIToFPInst(
1841 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1842) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001843 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001844}
1845
1846FPToUIInst::FPToUIInst(
1847 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1848) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001849 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001850}
1851
1852FPToUIInst::FPToUIInst(
1853 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1854) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001855 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001856}
1857
1858FPToSIInst::FPToSIInst(
1859 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1860) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001861 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001862}
1863
1864FPToSIInst::FPToSIInst(
1865 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1866) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001867 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001868}
1869
1870PtrToIntInst::PtrToIntInst(
1871 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1872) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001873 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001874}
1875
1876PtrToIntInst::PtrToIntInst(
1877 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1878) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001879 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001880}
1881
1882IntToPtrInst::IntToPtrInst(
1883 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1884) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001885 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001886}
1887
1888IntToPtrInst::IntToPtrInst(
1889 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1890) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001891 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001892}
1893
1894BitCastInst::BitCastInst(
1895 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
1896) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001897 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001898}
1899
1900BitCastInst::BitCastInst(
1901 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
1902) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001903 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001904}
Chris Lattnerf16dc002006-09-17 19:29:56 +00001905
1906//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00001907// CmpInst Classes
1908//===----------------------------------------------------------------------===//
1909
1910CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
1911 const std::string &Name, Instruction *InsertBefore)
Reid Spencer542964f2007-01-11 18:21:29 +00001912 : Instruction(Type::Int1Ty, op, Ops, 2, Name, InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00001913 Ops[0].init(LHS, this);
1914 Ops[1].init(RHS, this);
1915 SubclassData = predicate;
1916 if (op == Instruction::ICmp) {
1917 assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
1918 predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
1919 "Invalid ICmp predicate value");
1920 const Type* Op0Ty = getOperand(0)->getType();
1921 const Type* Op1Ty = getOperand(1)->getType();
1922 assert(Op0Ty == Op1Ty &&
1923 "Both operands to ICmp instruction are not of the same type!");
1924 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00001925 assert((Op0Ty->isInteger() || isa<PointerType>(Op0Ty)) &&
Reid Spencerd9436b62006-11-20 01:22:35 +00001926 "Invalid operand types for ICmp instruction");
1927 return;
1928 }
1929 assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
1930 assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
1931 "Invalid FCmp predicate value");
1932 const Type* Op0Ty = getOperand(0)->getType();
1933 const Type* Op1Ty = getOperand(1)->getType();
1934 assert(Op0Ty == Op1Ty &&
1935 "Both operands to FCmp instruction are not of the same type!");
1936 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00001937 assert(Op0Ty->isFloatingPoint() &&
Reid Spencerd9436b62006-11-20 01:22:35 +00001938 "Invalid operand types for FCmp instruction");
1939}
1940
1941CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
1942 const std::string &Name, BasicBlock *InsertAtEnd)
Reid Spencer542964f2007-01-11 18:21:29 +00001943 : Instruction(Type::Int1Ty, op, Ops, 2, Name, InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00001944 Ops[0].init(LHS, this);
1945 Ops[1].init(RHS, this);
1946 SubclassData = predicate;
1947 if (op == Instruction::ICmp) {
1948 assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
1949 predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
1950 "Invalid ICmp predicate value");
1951
1952 const Type* Op0Ty = getOperand(0)->getType();
1953 const Type* Op1Ty = getOperand(1)->getType();
1954 assert(Op0Ty == Op1Ty &&
1955 "Both operands to ICmp instruction are not of the same type!");
1956 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00001957 assert(Op0Ty->isInteger() || isa<PointerType>(Op0Ty) &&
Reid Spencerd9436b62006-11-20 01:22:35 +00001958 "Invalid operand types for ICmp instruction");
1959 return;
1960 }
1961 assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
1962 assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
1963 "Invalid FCmp predicate value");
1964 const Type* Op0Ty = getOperand(0)->getType();
1965 const Type* Op1Ty = getOperand(1)->getType();
1966 assert(Op0Ty == Op1Ty &&
1967 "Both operands to FCmp instruction are not of the same type!");
1968 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00001969 assert(Op0Ty->isFloatingPoint() &&
Reid Spencerd9436b62006-11-20 01:22:35 +00001970 "Invalid operand types for FCmp instruction");
1971}
1972
1973CmpInst *
1974CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
1975 const std::string &Name, Instruction *InsertBefore) {
1976 if (Op == Instruction::ICmp) {
1977 return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name,
1978 InsertBefore);
1979 }
1980 return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name,
1981 InsertBefore);
1982}
1983
1984CmpInst *
1985CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
1986 const std::string &Name, BasicBlock *InsertAtEnd) {
1987 if (Op == Instruction::ICmp) {
1988 return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name,
1989 InsertAtEnd);
1990 }
1991 return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name,
1992 InsertAtEnd);
1993}
1994
1995void CmpInst::swapOperands() {
1996 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
1997 IC->swapOperands();
1998 else
1999 cast<FCmpInst>(this)->swapOperands();
2000}
2001
2002bool CmpInst::isCommutative() {
2003 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2004 return IC->isCommutative();
2005 return cast<FCmpInst>(this)->isCommutative();
2006}
2007
2008bool CmpInst::isEquality() {
2009 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2010 return IC->isEquality();
2011 return cast<FCmpInst>(this)->isEquality();
2012}
2013
2014
2015ICmpInst::Predicate ICmpInst::getInversePredicate(Predicate pred) {
2016 switch (pred) {
2017 default:
2018 assert(!"Unknown icmp predicate!");
2019 case ICMP_EQ: return ICMP_NE;
2020 case ICMP_NE: return ICMP_EQ;
2021 case ICMP_UGT: return ICMP_ULE;
2022 case ICMP_ULT: return ICMP_UGE;
2023 case ICMP_UGE: return ICMP_ULT;
2024 case ICMP_ULE: return ICMP_UGT;
2025 case ICMP_SGT: return ICMP_SLE;
2026 case ICMP_SLT: return ICMP_SGE;
2027 case ICMP_SGE: return ICMP_SLT;
2028 case ICMP_SLE: return ICMP_SGT;
2029 }
2030}
2031
2032ICmpInst::Predicate ICmpInst::getSwappedPredicate(Predicate pred) {
2033 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002034 default: assert(! "Unknown icmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002035 case ICMP_EQ: case ICMP_NE:
2036 return pred;
2037 case ICMP_SGT: return ICMP_SLT;
2038 case ICMP_SLT: return ICMP_SGT;
2039 case ICMP_SGE: return ICMP_SLE;
2040 case ICMP_SLE: return ICMP_SGE;
2041 case ICMP_UGT: return ICMP_ULT;
2042 case ICMP_ULT: return ICMP_UGT;
2043 case ICMP_UGE: return ICMP_ULE;
2044 case ICMP_ULE: return ICMP_UGE;
2045 }
2046}
2047
Reid Spencer266e42b2006-12-23 06:05:41 +00002048ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2049 switch (pred) {
2050 default: assert(! "Unknown icmp predicate!");
2051 case ICMP_EQ: case ICMP_NE:
2052 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2053 return pred;
2054 case ICMP_UGT: return ICMP_SGT;
2055 case ICMP_ULT: return ICMP_SLT;
2056 case ICMP_UGE: return ICMP_SGE;
2057 case ICMP_ULE: return ICMP_SLE;
2058 }
2059}
2060
2061bool ICmpInst::isSignedPredicate(Predicate pred) {
2062 switch (pred) {
2063 default: assert(! "Unknown icmp predicate!");
2064 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2065 return true;
2066 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2067 case ICMP_UGE: case ICMP_ULE:
2068 return false;
2069 }
2070}
2071
Reid Spencerd9436b62006-11-20 01:22:35 +00002072FCmpInst::Predicate FCmpInst::getInversePredicate(Predicate pred) {
2073 switch (pred) {
2074 default:
2075 assert(!"Unknown icmp predicate!");
2076 case FCMP_OEQ: return FCMP_UNE;
2077 case FCMP_ONE: return FCMP_UEQ;
2078 case FCMP_OGT: return FCMP_ULE;
2079 case FCMP_OLT: return FCMP_UGE;
2080 case FCMP_OGE: return FCMP_ULT;
2081 case FCMP_OLE: return FCMP_UGT;
2082 case FCMP_UEQ: return FCMP_ONE;
2083 case FCMP_UNE: return FCMP_OEQ;
2084 case FCMP_UGT: return FCMP_OLE;
2085 case FCMP_ULT: return FCMP_OGE;
2086 case FCMP_UGE: return FCMP_OLT;
2087 case FCMP_ULE: return FCMP_OGT;
2088 case FCMP_ORD: return FCMP_UNO;
2089 case FCMP_UNO: return FCMP_ORD;
2090 case FCMP_TRUE: return FCMP_FALSE;
2091 case FCMP_FALSE: return FCMP_TRUE;
2092 }
2093}
2094
2095FCmpInst::Predicate FCmpInst::getSwappedPredicate(Predicate pred) {
2096 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002097 default: assert(!"Unknown fcmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002098 case FCMP_FALSE: case FCMP_TRUE:
2099 case FCMP_OEQ: case FCMP_ONE:
2100 case FCMP_UEQ: case FCMP_UNE:
2101 case FCMP_ORD: case FCMP_UNO:
2102 return pred;
2103 case FCMP_OGT: return FCMP_OLT;
2104 case FCMP_OLT: return FCMP_OGT;
2105 case FCMP_OGE: return FCMP_OLE;
2106 case FCMP_OLE: return FCMP_OGE;
2107 case FCMP_UGT: return FCMP_ULT;
2108 case FCMP_ULT: return FCMP_UGT;
2109 case FCMP_UGE: return FCMP_ULE;
2110 case FCMP_ULE: return FCMP_UGE;
2111 }
2112}
2113
Reid Spencer266e42b2006-12-23 06:05:41 +00002114bool CmpInst::isUnsigned(unsigned short predicate) {
2115 switch (predicate) {
2116 default: return false;
2117 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2118 case ICmpInst::ICMP_UGE: return true;
2119 }
2120}
2121
2122bool CmpInst::isSigned(unsigned short predicate){
2123 switch (predicate) {
2124 default: return false;
2125 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2126 case ICmpInst::ICMP_SGE: return true;
2127 }
2128}
2129
2130bool CmpInst::isOrdered(unsigned short predicate) {
2131 switch (predicate) {
2132 default: return false;
2133 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2134 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2135 case FCmpInst::FCMP_ORD: return true;
2136 }
2137}
2138
2139bool CmpInst::isUnordered(unsigned short predicate) {
2140 switch (predicate) {
2141 default: return false;
2142 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2143 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2144 case FCmpInst::FCMP_UNO: return true;
2145 }
2146}
2147
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002148//===----------------------------------------------------------------------===//
2149// SwitchInst Implementation
2150//===----------------------------------------------------------------------===//
2151
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002152void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002153 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002154 ReservedSpace = 2+NumCases*2;
2155 NumOperands = 2;
2156 OperandList = new Use[ReservedSpace];
2157
2158 OperandList[0].init(Value, this);
2159 OperandList[1].init(Default, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002160}
2161
Misha Brukmanb1c93172005-04-21 23:48:37 +00002162SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002163 : TerminatorInst(Instruction::Switch, new Use[SI.getNumOperands()],
2164 SI.getNumOperands()) {
2165 Use *OL = OperandList, *InOL = SI.OperandList;
2166 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
2167 OL[i].init(InOL[i], this);
2168 OL[i+1].init(InOL[i+1], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002169 }
2170}
2171
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002172SwitchInst::~SwitchInst() {
2173 delete [] OperandList;
2174}
2175
2176
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002177/// addCase - Add an entry to the switch instruction...
2178///
Chris Lattner47ac1872005-02-24 05:32:09 +00002179void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002180 unsigned OpNo = NumOperands;
2181 if (OpNo+2 > ReservedSpace)
2182 resizeOperands(0); // Get more space!
2183 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002184 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002185 NumOperands = OpNo+2;
2186 OperandList[OpNo].init(OnVal, this);
2187 OperandList[OpNo+1].init(Dest, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002188}
2189
2190/// removeCase - This method removes the specified successor from the switch
2191/// instruction. Note that this cannot be used to remove the default
2192/// destination (successor #0).
2193///
2194void SwitchInst::removeCase(unsigned idx) {
2195 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002196 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2197
2198 unsigned NumOps = getNumOperands();
2199 Use *OL = OperandList;
2200
2201 // Move everything after this operand down.
2202 //
2203 // FIXME: we could just swap with the end of the list, then erase. However,
2204 // client might not expect this to happen. The code as it is thrashes the
2205 // use/def lists, which is kinda lame.
2206 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
2207 OL[i-2] = OL[i];
2208 OL[i-2+1] = OL[i+1];
2209 }
2210
2211 // Nuke the last value.
2212 OL[NumOps-2].set(0);
2213 OL[NumOps-2+1].set(0);
2214 NumOperands = NumOps-2;
2215}
2216
2217/// resizeOperands - resize operands - This adjusts the length of the operands
2218/// list according to the following behavior:
2219/// 1. If NumOps == 0, grow the operand list in response to a push_back style
2220/// of operation. This grows the number of ops by 1.5 times.
2221/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
2222/// 3. If NumOps == NumOperands, trim the reserved space.
2223///
2224void SwitchInst::resizeOperands(unsigned NumOps) {
2225 if (NumOps == 0) {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002226 NumOps = getNumOperands()/2*6;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002227 } else if (NumOps*2 > NumOperands) {
2228 // No resize needed.
2229 if (ReservedSpace >= NumOps) return;
2230 } else if (NumOps == NumOperands) {
2231 if (ReservedSpace == NumOps) return;
2232 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002233 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002234 }
2235
2236 ReservedSpace = NumOps;
2237 Use *NewOps = new Use[NumOps];
2238 Use *OldOps = OperandList;
2239 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2240 NewOps[i].init(OldOps[i], this);
2241 OldOps[i].set(0);
2242 }
2243 delete [] OldOps;
2244 OperandList = NewOps;
2245}
2246
2247
2248BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
2249 return getSuccessor(idx);
2250}
2251unsigned SwitchInst::getNumSuccessorsV() const {
2252 return getNumSuccessors();
2253}
2254void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
2255 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002256}
Chris Lattnerf22be932004-10-15 23:52:53 +00002257
2258
2259// Define these methods here so vtables don't get emitted into every translation
2260// unit that uses these classes.
2261
2262GetElementPtrInst *GetElementPtrInst::clone() const {
2263 return new GetElementPtrInst(*this);
2264}
2265
2266BinaryOperator *BinaryOperator::clone() const {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002267 return create(getOpcode(), Ops[0], Ops[1]);
Chris Lattnerf22be932004-10-15 23:52:53 +00002268}
2269
Reid Spencerd9436b62006-11-20 01:22:35 +00002270CmpInst* CmpInst::clone() const {
Reid Spencerfcb0dd32006-12-07 04:18:31 +00002271 return create(getOpcode(), getPredicate(), Ops[0], Ops[1]);
Reid Spencerd9436b62006-11-20 01:22:35 +00002272}
2273
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002274MallocInst *MallocInst::clone() const { return new MallocInst(*this); }
2275AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); }
2276FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); }
2277LoadInst *LoadInst::clone() const { return new LoadInst(*this); }
2278StoreInst *StoreInst::clone() const { return new StoreInst(*this); }
2279CastInst *TruncInst::clone() const { return new TruncInst(*this); }
2280CastInst *ZExtInst::clone() const { return new ZExtInst(*this); }
2281CastInst *SExtInst::clone() const { return new SExtInst(*this); }
2282CastInst *FPTruncInst::clone() const { return new FPTruncInst(*this); }
2283CastInst *FPExtInst::clone() const { return new FPExtInst(*this); }
2284CastInst *UIToFPInst::clone() const { return new UIToFPInst(*this); }
2285CastInst *SIToFPInst::clone() const { return new SIToFPInst(*this); }
2286CastInst *FPToUIInst::clone() const { return new FPToUIInst(*this); }
2287CastInst *FPToSIInst::clone() const { return new FPToSIInst(*this); }
2288CastInst *PtrToIntInst::clone() const { return new PtrToIntInst(*this); }
2289CastInst *IntToPtrInst::clone() const { return new IntToPtrInst(*this); }
2290CastInst *BitCastInst::clone() const { return new BitCastInst(*this); }
2291CallInst *CallInst::clone() const { return new CallInst(*this); }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002292SelectInst *SelectInst::clone() const { return new SelectInst(*this); }
2293VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); }
2294
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002295ExtractElementInst *ExtractElementInst::clone() const {
2296 return new ExtractElementInst(*this);
2297}
2298InsertElementInst *InsertElementInst::clone() const {
2299 return new InsertElementInst(*this);
2300}
2301ShuffleVectorInst *ShuffleVectorInst::clone() const {
2302 return new ShuffleVectorInst(*this);
2303}
Chris Lattnerf22be932004-10-15 23:52:53 +00002304PHINode *PHINode::clone() const { return new PHINode(*this); }
2305ReturnInst *ReturnInst::clone() const { return new ReturnInst(*this); }
2306BranchInst *BranchInst::clone() const { return new BranchInst(*this); }
2307SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); }
2308InvokeInst *InvokeInst::clone() const { return new InvokeInst(*this); }
2309UnwindInst *UnwindInst::clone() const { return new UnwindInst(); }
Chris Lattner5e0b9f22004-10-16 18:08:06 +00002310UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();}